So I'm trying to query some posts from 1 category only and it does seem to be working. I tried get_post() also but no luck and after awhile I just caved and used query_posts (which is apparently bad practice). Here's my code:

<div id="primary">
            <div id="content" role="main">
                <?php query_posts('slug=cat1'); ?>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <div class="post">
                            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                                <?php the_title(); ?>
                        <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
                            <br />
                            <br />
                        <div class="entry">
                            <?php the_content(); ?>
                    <hr />
                <?php endwhile; else: ?>
                <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
                <?php endif; ?>
            </div><!-- #content -->
        </div><!-- #primary -->

The second endif; is from above I have a test against the page slug. But from my understanding the query_posts should populate $post with all the posts from that category but I'm still getting posts from uncatagorized and another category so it's not filtering at all... Suggestions?

Edit1 I tried using cat=0 and cat=1 also cat=2 but the only one that actually filters is the cat=1 which filters to uncategorized. All the others show all posts. Not sure why...

Edit2 query_posts( array ( 'category_name' => 'Category1', 'posts_per_page' => -1 ) ); returned with the "else" statement saying there was no posts and nothing fit my criteria. I pulled that straight from the codex?

2 Answers 2


Weird Problem!, try the following:

  • Make sure that the targeted categories are not empty.
  • If you are using custom post type,include it in the query 'post_type'=>'advertisement' for example.
  • If the targeted categories are special taxonomies,make sure to include it in the query : 'term'=>'cars' along with child 'cars'=>'toyota'.

    • in this example and just to make it clear : cars is like category and toyota is like uncategorized.

One last thing: trace the query by echoing the query to find out what is the exact arriving query:

echo $query_string;; place it in the top of the page before the <?php if ( have_pos... and right after the query_posts(..);

please tell us the results of the query in case it didn't work.

  • 1
    what are the category name, category slug and category ID of the category that you are trying to retrieve? have you read: codex.wordpress.org/Class_Reference/… ?
    – Michael
    Commented Oct 4, 2011 at 23:04
  • @Michael didn't get your question clearly, Would you explaining little more?
    – wdalhaj
    Commented May 5, 2012 at 0:22
  • never mind - the question was directed at Howdy_McGee - I just posted it below the wrong reply ;-(
    – Michael
    Commented May 5, 2012 at 10:49

Simple code to show all post when you dont have anything on your query_var (the output will be -1 to show all posts) and certain post by getting the query_var.

if(get_query_var('cat') == ''){
   $cat = (get_query_var('cat')) ? get_query_var('cat') : -1;}
   $cat = (get_query_var('cat'));}
$args = array('posts_per_page' => 5, 'paged' => $paged, 'cat' => $cat);

Hope it can helped you.

Edited: ofc you need to pass on your menu the 'cat' id into the URL.

Edited2: codex page

hooking into 'pre_get_posts' and altering the main query that way using is_main_query http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

its the preferred way... but the example above works like a champ!

Just make sure you call wp_reset_query() after you're done de 2nd loop.

  • query_posts should not be used to create secondary loops.
    – s_ha_dum
    Commented Feb 4, 2013 at 20:08
  • sry, i dont get it maybe im to fresh. Well for me this was the primary loop... although you just need to finish everyquery wp_reset_query() and everything should be fine i suppose. Commented Feb 5, 2013 at 10:57
  • Read the Codex page for query_posts. It is a bad way to modify the main Loop as well. wp_reset_query doesn't solve the issue. If it is the main query, using query_posts causes two queries to run when you only need one.
    – s_ha_dum
    Commented Feb 5, 2013 at 15:17
  • I think isnt an issue. But just bad practice. Codex suggests a lot of this double loop examples and recommend to end all open queries... seems bad practice that can be minimized but isnt a issue. Or it is and im see it wrong? Commented Feb 6, 2013 at 11:56
  • query_posts will work, sure, but it doubles the database load for the page. That is an issue, maybe not a fatal issue but an issue. Yes, I know the Codex uses query_posts a lot. It is community edited and much of that is probably years old.
    – s_ha_dum
    Commented Feb 6, 2013 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.