How to Display Only Posts from Certain Categories on Your Mainpage and Feeds
Apr 10th, 2008 | Category: Make Money BloggingThe hack will enable you to select which categories will have their posts displayed on the homepage and through feeds.
On my previous post about irrelevant posts, I mentioned the technique I use to give my readers an authority over off topic posts. Using the technique, they won’t have to counter any posts fall outside their concern.
Enough jabbering, here are the hacks I use:
- excluding posts from certain categories, they’ll be invisible on the main page and feed updates: put this code inside your functions.php.
function myFilter($query) {
if ($query->is_feed || $query->is_home) {
$query->set('cat','-151,-200,-520');
}
return $query;
}
add_filter('pre_get_posts','myFilter');
The code above works by eliminating posts from the category ID 151 and 200. You may want to change the values into your own category ID. Keep the dash (-) sign, by the way.
If you want to exclude the posts from one of either the main page or the feed, you can alter the code which says:
($query->is_feed || $query->is_home)
into this one for excluding the posts on the mainpage:
($query->is_home)
or this one, to make the posts invisible via feed updates:
($query->is_feed)
- making off topic posts displayed separately. As you can see on my mainpage, there is a section called “Various Buzz” containing posts unrelated to make money online tips. I group them below my relevant posts so that my readers won’t have to see them between relevant posts, but the “Various Buzz” posts are still visible for those who want to read them. In order to make that happen, first I made them invisible on the mainpage using the codes above, and then I added this block of code in my index.php:
<center><h2>Various Buzz</h2></center><br>
<strong><?php $my_query = new WP_Query('cat=151&showposts=5'); ?></strong>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p class="postinfo"><!--?php the_author_posts_link(); ?--> On <?php the_time('M j, Y') ?> | <?php edit_post_link('Edit', ' | ', ''); ?> | </p>
<div class="entry">
<?php the_excerpt(); ?><br><hr />
</div>
<?php endwhile; ?>
The core of the script has been bolded. That part should be written exactly. You can modify the values according to your own category ID (cat) and the number of posts you want to display (showposts). From the code above, you can see that I’m displaying 5 posts from category ID 151.
The other part of the code is just giving you a picture of where you can put the code. Since I want to display them on my mainpage, I put them on my index.php. Should you need to show them on the sidebar, you can place them on your sidebar.php.
Now, I hope that small slice of hack could do you and your readers some good!
Some codes contributed by zeo.unic.net.my
Technorati Tags: exclude post from a category,blogging,group posts in a category






That’s an interesting hack. I’m going to give it a try and see how it works out. Thanks.
Just let me know if there’s a problem.
When I update the function there’s an error
Fatal error: Call to undefined function add_filter()
I use WP 2.3.3
Hello, radha.
It looks like the cause was WP’s auto change to single quotes. That’s why if we copy-paste the codes, they won’t work properly.
I’m sorry I didn’t notice this before, but I’ve found a solution.
Now the codes can be safely copy-pasted without altering anything. Give it a try and if you’re stuck again, let me know.
That’s pretty useful! I am thinking of using something just like this on a new blog I am working on, this should be put to good use… thanks for the info!
Jims last blog post..ClickBooth Affiliate Network Review
Good post im might give this a try on my other blogs.
David Hobsons last blog post..104 Social Bookmarking Websites to Submit Too