Build Your Own Business Website header image

How to Exclude Posts from the Blog Page using Categories and Tags

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

In this session, we begin with a short demonstration on how to use existing WordPress functionality to add categories to the WordPress Menu and to the sidebar using a widget. Then we discuss the ability to exclude posts by category in which case you can use our BYOB Thesis Exclude Categories From Posts plugin.

You can also exclude posts by Tag. Rick will be updating his plugin in the next few days to give option to exclude Tags from Post, but right now you can add the code shown below to your custom_functions.php file to exclude tags from your blog post.

/*******************************************************************************
* These functions customize the Blog Page
********************************************************************************/
function home_page_custom_query(){
    if(is_home()){
       query_posts(array(
           'category__not_in' => array(19068,19069,19071,19056),
           'tag__not_in' => array(19076,21,22,35)
           )
       );
    }
}
add_action('thesis_hook_before_content', 'home_page_custom_query');

Video Transcript

Rick: Just barely maybe

Member: Ok, I’ll try the…

Rick: That’s perfect.

Member: Is that better, ok?

Rick: That’s better.

Member: Well, glad I caught you on the upswing

Rick: Hahaha, absolutely.

Member: I don’t know if you remember but about year ago we started talking about a Community News Site. And I’m trying now and I’ve caught that up and it was going and it’s functioning and it’s doing very well. But I’m trying to help a local bicycle club out with their website now. And I want to do something that is not as complicated as that, but still uses that functionality of putting blog, particular category of blog post on particular pages. So for instance there’d be a Home page that might draw in and Announcements for the club, but then another page that might have news about mountain biking and another one for a road riding and another one for road racing, things like that. And I’m trying to use the same code that we use before, but I’m thinking that it may not be necessary to use custom post types because I don’t need that level of hierarchy for this.

Rick: Right. It’s not. No, I mean WordPress has the built-in functionality to do what you want to do without adding custom post types to the equation. Here’s all the video that we did last year though.

Member: Yes, I watched them last night.

Rick: So, the way you would approach this though is use the post functionality and everything that you want to show up on the post page, I mean anything you want to exclude from the post page, you could exclude by using the plugin that we just walked through a moment ago. You know, my Exclude Category From Posts Plugin and that will exclude those categories from the post page. However, anytime you want to look at those posts all you have to do is go to an archive page for that category.

So for example, actually what I’m going to do here is I’m going to put a category widget in here quickly so the category will show up. Show hierarchy, show categories or let’s see… I’m going to call it articles. So you can distinguish here and then…so now we’ve got this list of all the categories on my site. And if I want to look at my Green Apples category, I can click on that and it brings up all the post that categorized as Green Apples. Now, I could add this to my menu by taking this URL and making it a link or I could add it to my menu by using the WordPress Menu system.

Actually, I see now, let’s see how do I have this thing set up? Yeah, right now I have it setup using Thesis Nav Menu, but if I uses the WordPress Nav Menu and then go back over to menus. Actually I have category menu already setup here, choose that as my primary menu. And now I have these different categories in the menu. Clicking on them will take you to an archive page that has all of those posts on it. This is built-in automatic WordPress behavior. You don’t have to…

Member: So then I would use my Home Page, my Front Page would be the blog page.

Rick: Ok and so you either show all of your post on that page or you decide some of the post you can’t want there. Some you only want a certain class of post there and you could use the Exclude Categories from Posts to exclude them. And then you could you some mechanism either by putting the categories in the menu or by having a widget that has the categories. You can use some tool like that to access those categories.

Member: Yes, I think that is the easiest way to go. There might be for instance, some mountain biking posts that…I was hoping if I could use tags or some second level hierarchy to pull them to the front. So those if there is a new trail being built in town that might move to the front. But if it’s just an update about the regular weekend trail clearing then maybe it would…so that if your category is mountain biking there could be a tag for news or there could be another tag for clean up and there could be another tag for racing or something like that and only some of those will go to the front.

Rick: Yes, you could exclude posts by tag rather than excluding posts by category.

Member: How do you do that?

Rick: Well…

Member: it takes some coding?

Rick: Yeah, it does, but it’s not hard coding. And I could actually update my plugin to give you the choice of doing that. Because I could easily do that, add that to this feature where it will exclude… it will have categories to exclude from the post page and then it would have tags to exclude from the post page. And if you didn’t select any categories, but you just selected tags it will display all the categories but none of the… but it would exclude any of the tags that you listed. So you could use tags essentially for visibility. To display on the front page or not. It would be better to use a category on that because the way it should work is every on post that only has a single category.

Member: Right.

Rick: And you know, if you give me a couple of days I’d be glad to upgrade this plugin. So that it will includes the ability to exclude by tag instead of by category.

Member: Ok, that’ll be great. Would it be possible for you to on the answers to put the code so I could put it on the custom file when it’s done?

Rick: Yes, in fact I can show what that code looks like, because I use a tag to exclude categories from my blog page. I exclude my video posts from my blog page. And so, let’s see it’s probably here. Ok, so this is the code that I use and I have four categories that I don’t want to display on the home page. And I have four tags that I don’t want to display on the home page. And so, what I do is add this query to my home page.

I’m going to post this on the forum for you, so you can just copy and paste this. Ok, I think today, this is an old piece of code of mine…today I’m going to change this some way. Will just go and piece this together and then let’s see…Ok so, I’m going to clean this out and so what we’ll do here is ask a question. If is_home then change query post essentially, change the query post array to say category_not_in with these categories and tag_not_in with these tags. And then the function name is home_page_custom_query and so what you do is you hook home_page_custom_query to thesis_hook_before_content. And that’s all there is to it. Not quite sure why that’s throwing an error. Probably because there’s something wrong with this file. Yeah, that’s better.

Ok, so it’s really simple. All you’re doing is you’re doing is changing the query post to say that it’s category_not_on and tag_not_in and that’s how you exclude…

Member: How can you find the tag number?

Rick: Well it’s the same way how to find the category number, it’s just the number.

Member: It’s just showing at the bottom of…ok.

Rick: Yes, so for example. Let’s go to my site in this case. And if you just hover over to the tag, you can see down there at the bottom is tag 19135. You see it on my screen down there way at the bottom?

Member: Yeah, I do. Ok

Rick: Yeah and this is tag id 7 so these are the ones that you would choose to exclude. So this is an example of the code, I mean a living working example of a code where categories are excluded and tags are excluded from the home page display.

Member: Very good. I think that’s it.

Rick: I will come back over here to the forum and paste that under Live Answer Code. And then you can just get it from there. Let’s see, add topic, Exclude Posts from the Home Page by Category and tag.

Member: Good, that would be a big help.

Rick: Well, obviously you have to manually change the code every time you want this to happen. Where with my plugin, you can check or uncheck boxes but nevertheless I think this a fairly straight forward procedure.

0 Comments… add one

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
THIS IS A LIMITED TIME OFFER!
Subscription Options
0 comments… add one

Leave a Comment