Hide Posts Based on Category

Apr 21, 2009 at 9:33 PM
Hi all

im having trouble working out how i could hide posts that have been allocated to a certain category. Dont ask why i need to do such a thing!! but i do...

basically when a blog entry is made and assigned to my 'special category' I want it to remain hidden from the publsihed blog posts, even though it may have been set as 'Published' when the entry is made.

is that possible to do?

thanks
jj
Coordinator
Apr 21, 2009 at 11:45 PM
I would just modify Add_entry.aspx.cs, and set IsPublished equal to false if the hidden category was selected, to enforce posts of these categories from being treated as not published.  If I'm not mistaken, there's no difference in outcome with this approach.

If you want to do this, there's already a btnSave_Click event handler with a foreach loop for categories in Add_entry.aspx.cs.  The code below is a modified version of that.  You'd want to replace "your hidden category" below with your specific category.

foreach (ListItem item in cblCategories.Items)
{
    if (item.Selected)
    {
        post.Categories.Add(Category.GetCategory(new Guid(item.Value)));
        if (item.Text.Equals("your hidden category", StringComparison.OrdinalIgnoreCase))
            post.IsPublished = false;
    }
}
Apr 22, 2009 at 11:03 PM
BenAmada to my rescue once again....

I will give your suggestion a try it sounds like a good solution... my only hassle is that i still need the publish non publish option as well ie... with the 'Special Category' i will still have published and un-published posts.  The published ones i'll need to display in a special widget, but not in the normal posts.

I should have explained that in my first post but I think i can work around this using you suggestion, with a bit of training for my users.. ie dont make an entry for that category unless you mean it or save it to another category unpublished until ready, but that wont go down well im sure.  I may try adding a second publish flag perhaps... so that i can use your option to make sure I dont have to upset the code in the rest of the blog engine... easy for me to do if only the blog i am working was using SQL rather than XML.  Im not  to hot with XML! so any further sugestions on this would be grately apprecated. Basically is it easy to add another Flag to the blog entries XML without upsetting anything else?

Cheers,
j
Coordinator
Apr 23, 2009 at 12:13 AM
Adding another flag would be pretty easy.  It would require modifying the BE core and a recompile.

We can create a new property called IsHidden and then modify the IsVisible property's logic to check the IsHidden value.  If we model the new IsHidden property after the existing IsPublished property, it makes it easier to set it up since they're the same data type, etc.  (NOTE: At the bottom of this message, I describe a possibly better method than adding a new property, depending on your possible future needs).

1. In Post.cs (in the BE core), add the new IsHidden property:

private bool _IsHidden;
/// <summary>
/// Gets or sets the IsHidden value of the post.
/// </summary>
public bool IsHidden
{
    get { return _IsHidden; }
    set
    {
        if (_IsHidden != value) MarkChanged("IsHidden");
        _IsHidden = value;
    }
}

2. In Post.cs, we'll modify the existing IsVisible property.  The modified version is below:

/// <summary>
/// Gets whether or not the post is visible or not.
/// </summary>
public bool IsVisible
{
    get
    {
        if (!IsHidden && (IsAuthenticated || IsPublished))
            return true;

        return false;
    }
}

3. Since you're using XML storage, we'll modify the Posts.cs file in the BE core in the Providers\XmlProvider folder.  In the SelectPost method, after the post.IsPublished property is set, we'll add this new code:

if (doc.SelectSingleNode("post/ishidden") != null)
    post.IsHidden = bool.Parse(doc.SelectSingleNode("post/ishidden").InnerText);

4. In the same Posts.cs file, in the InsertPost method, we'll add this after the post.IsPublished property is written.

writer.WriteElementString("ishidden", post.IsHidden.ToString());

5. Then re-compile the BE core.

6. In the Add_Entry.aspx.cs file under admin\Pages, in the btnSave_Click event handler, we'll modify the existing code (like yesterday) where the selected categories are checked for

bool isHiddenCategory = false;
post.Categories.Clear();

foreach (ListItem item in cblCategories.Items)
{
    if (item.Selected)
    {
        post.Categories.Add(Category.GetCategory(new Guid(item.Value)));
        if (item.Text.Equals("your hidden category", StringComparison.OrdinalIgnoreCase))
            isHiddenCategory = true;
    }
}

post.IsHidden = isHiddenCategory;

*** That should be it.  You'll still need logic in your widget to retrieve posts that wouldn't normally be displayed because IsVisible returns false.

NOTE: It occurred to me that the same thing can be accomplished here without adding the new IsHidden property.  Instead of adding the IsHidden property, you could just modify the IsVisible property in the Post.cs file in the BE core as shown below.  A recompile of the core is required.

/// <summary>
/// Gets whether or not the post is visible or not.
/// </summary>
public bool IsVisible
{
    get
    {
        if (IsAuthenticated || IsPublished)
        {
            foreach (Category c in this.Categories)
            {
                if (c.Title.Equals("your hidden category"))
                    return false;
            }
            return true;
        }

        return false;
    }
}
Apr 23, 2009 at 12:48 PM
hi BemAmada,

i like the sound of your option 2 it makes sense to ome I will give that a go if It doesn't work out I'll be back!

i can then just pick up on the posts that are assigned to my Category and also check there visisble status.  Does pose a question in my mind though as to why there is an IsVisible as well as an IsPublished property.. ill give t a go all the same.

thanks agan...
j
Coordinator
Apr 23, 2009 at 5:27 PM
The IsVisible property is a handy property in BE.  As you can sort of see from my last post, the existing IsVisible property will consider an item "visible" even if its "unpublished" if the person is logged in.  This is why you can see unpublished posts if you're logged in -- because of the IsVisible property.
Apr 24, 2009 at 9:38 AM
I see now how it works now thats really helpful thanks BenAmada... I will give it a go although Im not too experinced working with the core...
Cheers.
Apr 25, 2009 at 11:36 PM
I was able to update the core and get the desired result but...

I use a serperate Visual Studio 2008 solution to maitain my core...  (i'm sure in the past this has been ok, so not sure what ive done different this time)  the solution builds ok and i figured i could thenm copy the 3 files BlogEngine.Core.dll, BlogEngine.Core.pdb, BlogEngine.Core.XML into the bin folder of the web application I need the updated core. 

bt when i now bulid that solution i get about a dozon warnings alng te lines of;
Could not find schema information for the element 'BlogEngine'

pointng to lines within the web.config....
is this somethng i need to worry about?

cheers,
jj
Coordinator
Apr 26, 2009 at 1:44 AM
I don't recall seeing those warnings when doing a build of the solution before.  But I have seen other warnings.  If everything runs fine, I wouldn't worry about the warnings.  Sometimes warnings like those are given at the time of building the solution, but at runtime, the 'BlogEngine' element is known.
Apr 26, 2009 at 9:56 AM
yep all seems to run ok...
cheers.