Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show only one category of post on Home & Archive pages WordPress


Hi all,

Question regarding controlling what categories show up as visible:

I only want my "news" stories to be visible to my site visitors (this is the post category called "News", ID=1.) I want to hide all other categories of post on my home page and in my archive list of posts.

Can I hardcode my php to permanently look for only for posts categorized as "news" and ignore all other posts?

(This may seem like a strange request, but I am using different post categories to associate mp3s with different podcast feeds and I don't want these to show up on home or in the archives. Those post categories are only to feed the podcasts)

When you reply, could you let me know what code to look for in my home and archives pages and let me what to replace? I'm not much of a developer.

Thanks in advance!!


Answers (4)


Will Anderson answers:

The problem with using query_posts is that is overrides any other query variables that might be applied (like date queries for the archive for example). A better solution is to use a filter. This code is based on a similar article I wrote about limiting categories in RSS feeds. [[LINK href=""]]article[[/LINK]]

The following code will do the same thing, but on on the home and archive pages instead.

function only_news_cat($query) {
if ( is_home() || is_archive() ) {
$query->set('category_name', 'News');
return $query;
add_filter('pre_get_posts', 'only_news_cat');

I would suggest putting that code in your functions.php file. You may need to change the capitalization of the word "News", depending on whether the actual category name is capitalized.

Will Anderson

Kelli Anderson comments:

Hi Will,

When I post your code into the end of my wp-includes/functions.php file, I get this error:
(when I reload the site)

Fatal error: Call to undefined function add_filter() in /home/merrifieldrecords/ on line 3654



Will Anderson comments:

Oh, not wp-includes/functions.php, but the functions.php in your theme folder (if that file doesn't exist, you should create it).

Kelli Anderson comments:

Cool thanks!

Worked like a charm


Oleg Butuzov answers:

Code can be inserted before while_posts running.

if you have home.php or archive.php you need to edit it and insert this code before...
if you havn't this files - use index.php

if ( is_home() || is_archive()) {

this how code shoudl looks at least in idea...

if ( is_home() || is_archive()) {

<?php while (have_posts()) : the_post(); ?>
<!-- your loop code -->
<?php endwhile; ?>


Maor Barazany answers:

Hi Kelli,

Yes, it is possible and it's quite easy to do.

Just add a query_posts before the loop.
The loop starts with the code something like -
if (have_posts()) : while (have_posts()) : the_post(); ?>

So a line before the loop starts just add -

<?php query_posts('showposts=4&cat=1');?>

cat = 1 means that it will show only category number 1.
If you write instead cat=-1, so all categories except number 1 will be shown.
You may have more than one category, just add cats separated with commas - cat=1,2,x,y,z

showposts=4 - this means that only 4 posts will be shown, you may use -1 and it will show all posts, or any other number you like.

Good luck!



Erez S answers:

echo $category[0]->cat_name;
Replace the following code on your index.php and archive.php files with the code above:
(Something like that,it changes from theme to theme)