Forums

[resolved] Query Posts Question (5 posts)

  1. acornrevolution
    Member
    Posted 2 years ago #

    I am using the following code on my front page for querying recent posts. I am excluding category 3, however this also excludes any posts that have several categories, one of which is category 3 (i.e. a post in category 15 and 3). How can I include these kinds of posts?

    <?php
        $recentPosts = new WP_Query();
        $recentPosts->query('showposts=5&cat=-3');
    ?>
    <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
  2. danoprey
    Member
    Posted 2 years ago #

    Don't know if this is the best solution, but the obvious answer is to limit it to showing all the categories except category 3.

    e.g.

    <?php
        $recentPosts = new WP_Query();
        $recentPosts->query('showposts=5&cat=1,2,4,5');
    ?>
  3. Shane G.
    Member
    Posted 2 years ago #

    Hi,

    Check with this plugin:

    http://wordpress.org/extend/plugins/query-posts/

    Thanks,

    Shane G.

  4. acornrevolution
    Member
    Posted 2 years ago #

    danoprey, that worked! I wasn't going to do it at first, since I have 9 categories, but they are fixed, so I wont have to worry about adding more. Thanks!

    Shane G., this wasn't for the sidebar, but thanks!

  5. nimitmangal
    Member
    Posted 1 year ago #

    Lets say I have a category with ID=3 and it's many more sub-categories.
    But I want to display posts only from catID=3 not it's sub-cat...
    How can we modify this code...?

    $recentPosts->query('showposts=5&cat=1,2,4,5');

    Nimit

Topic Closed

This topic has been closed to new replies.

About this Topic