WordPress.org

Ready to get started?Download WordPress

Forums

Multiple category_name or alternative? (19 posts)

  1. davidjg
    Member
    Posted 9 years ago #

    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.

  2. davidjg
    Member
    Posted 9 years ago #

    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.

  3. nateomedia
    Member
    Posted 9 years ago #

    query_posts('cat=12,14,15,16')

  4. davidjg
    Member
    Posted 9 years ago #

    I get a "Sorry, no posts matched your criteria".

  5. nateomedia
    Member
    Posted 9 years ago #

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

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

    <?php
    $posts = 3; //Number of posts to display
    query_posts('cat=##,##,##,##&showposts='.$posts);
    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.

  6. davidjg
    Member
    Posted 9 years ago #

    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.

  7. davidjg
    Member
    Posted 9 years ago #

    Nope, it's not - just commented them out, same result.

  8. nateomedia
    Member
    Posted 9 years ago #

    Maybe post your loop?

  9. davidjg
    Member
    Posted 9 years ago #


    <?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?

  10. nateomedia
    Member
    Posted 9 years ago #

    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?

  11. davidjg
    Member
    Posted 9 years ago #

    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.

  12. Kafkaesqui

    Posted 9 years ago #

    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(); ?>

  13. davidjg
    Member
    Posted 9 years ago #

    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.

  14. Kafkaesqui

    Posted 9 years ago #

    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:

    http://pastebin.com/

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

  15. davidjg
    Member
    Posted 9 years ago #

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

  16. Kafkaesqui

    Posted 9 years ago #

    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.

  17. davidjg
    Member
    Posted 9 years ago #

    Even with Most Popular commented out, I still get "Sorry, no posts matched your criteria". Odd.

    Only plugins activated are "Customisable Post Listings" and "Popularity Contest"

    Could it be a server issue (although I can't think why...)?

    If it helps:

    • MySQL Version - 4.0.22-standard
    • PHP Version - 4.3.9

    Anything else that would be useful to know?

    EDIT:

    Ok, I've ascertained that it's only when we use multiples that there's a problem - using $my_query = new WP_Query('cat=2&showposts=10'); (there are no posts under category 1) returned results exactly as expected.

    Furthermore, I believe the reason I got a "Sorry, no posts matched your criteria" was because there were no posts in category 1. When I do $my_query = new WP_Query('cat=2,3&showposts=10');, I only get category 2 posts shown. So obviously it's only getting the posts from the first category listed. Which is a bit strange. Would there be any way of getting round this?

  18. nateomedia
    Member
    Posted 9 years ago #

    Do you plan to have empty categories? Why? This sounds like a bug in any case, but try posting some content to each category you call and see if that fixes things. Since you probably won't have empty categories, it sounds like this might solve your problem.

  19. davidjg
    Member
    Posted 9 years ago #

    I found the solution. I needed to upgrade to WP 1.5.2 for it to work - now it's fine.

    So, for anyone else with a similar problem: UPGRADE!

    Also, although not sure how far this is relevant, I did post in category one (but this didn't make a difference when running WP 1.5).

    Thanks for all your help guys!

Topic Closed

This topic has been closed to new replies.

About this Topic