Support » Plugins and Hacks » Multiple category_name or alternative?

Multiple category_name or alternative?

  • Hi;

    I’m using 'query_posts('category_name=X')' in order to filter which posts display. Two questions:

    1. Is it possible to list multiple category names, and if so, how do I separate them? (commas, spaces etc)
    2. A better alternative would be to use category ids – I’ve tried cat_id, but no luck. What’s the correct way of doing this, and again, how would I list multiples?

    Thanks for any help you can give.

Viewing 15 replies - 1 through 15 (of 18 total)
  • Ok, sort of answered part of my own question for number two, using cat, but now I still need to know how to list multiple categories.


    I get a “Sorry, no posts matched your criteria”.

    Works for me. You are, of course, substituting in your category numbers, correct?

    Here’s the whole structure of the loop I’m running:

    $posts = 3; //Number of posts to display
    while ( have_posts() ) : the_post(); { ?>

    //Insert article content here

    <?php } endwhile; ?> //

    I use $posts as a counter in the loop so that the last post can have a special css class.

    Even after using your loop, still “no posts matched your criteria”. Could it be because I’m using ColdForged asides to get three categories after the loop?

    I’m trying to display categories 10, 11 and 12 in three columns underneath my main content – so am trying to exclude those three categories from being show. Using this Codex entry, I could successfully exclude them – but, this meant that no posts whatsoever would be displayed if anything in cats 10, 11 or 12 where the latest posts.

    Nope, it’s not – just commented them out, same result.

    Maybe post your loop?

    <?php query_posts('cat=1,2,3,4,6,7,8,9&showposts=10'); ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    //Post content

    <?php endwhile; ?>

    And later on, I have three of these:

    <?php c2c_get_recent_posts('10',"%post_content%
    (%comments_count_url%)",'12'); ?>

    The 12 at the end refers to the category number – I’ve got one each for 10,11 and 12.

    Any of that help?

    So, are posts showing up correctly for the loop? Is the problem just with your asides? Maybe you need to rewind the loop for your asides?

    No, the problem’s with the loop – when I use query_posts('cat=2&showposts=10') or whatever, but only one cat, it works fine – same if I use 'cat=-10'. However, multiples of either don’t work.

    Try setting up a new WP_Query instance, to avoid any conflicts with other loops:

    <?php $my_query = new WP_Query('cat=1,2,3,4,6,7,8,9&showposts=10'); ?>
    <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>

    Ok, a whole raft of issues. Here they are chronologically:

    I copied and pasted the code, putting it before the beginning of the loop (was this placement wrong?). This resulted in a parse error, with an unexpected $ on line 140. Unfortunately, line 140 is the last line of my document, and blank.

    I then commented out the loop and the endwhile;but I left the div class="post" etc. Again with the parse error.

    However, I have so many commented-out code snippets that I’m considering re-writing my index so it’s clean. If the solution to my problem is not immediately apparent, would you mind telling me what order the new query should come etc – e.g. standard loop, custom query etc – so I get it write. Many thanks for your help so far.

    Your placement is not wrong, but I assumed you would only replace the query_posts portion of your code above with my suggestion. Here’s what it should look like in total:

    <?php $my_query = new WP_Query('cat=1,2,3,4,6,7,8,9&showposts=10'); ?>
    <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>

    //Post content

    <?php endwhile; endif; ?>

    Note the addition of endif; to close out if ($my_query->have_posts()).

    If this still cracks, paste your entire template here:


    and provide a link back to it in a reply. Can’t beat checking the code in context…

    Ok, with this exact code I am getting a “Sorry, no posts matched your criteria”. The asides are still working fine though.

    I can’t duplicate what you’re seeing. I’ve commented out akpc_most_popular() (for the obvious reason), but otherwise running your template as is I display posts from the WP_Query, and under each c2c_get_recent_posts() section.

    I’m not sure what you’re running into here.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Multiple category_name or alternative?’ is closed to new replies.