• Resolved gbrown88

    (@gbrown88)


    Hi,

    I am trying to create a list of posts that shows all posts that are in both the same category. Example:

    Post 1: Categories Are: A & B
    Post 2: Categories Are: B
    Post 3: Categories Are: B & C
    Post 4: Categories Are: B
    Post 5: Categories Are: A & B

    My list would only show:

    Post 1
    Post 5

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • This post on Display Certain Categories Within WordPress gives basic information on using parameters to query_posts() to select categories.

    If you need more specific help than that, please post a link to your site, your theme name, and more detail about how you want to display your posts (probably on a page using a special template).

    Thread Starter gbrown88

    (@gbrown88)

    Yeah, I am using a page template. What I want basically is to show only posts that are in two categories, but they have to be tagged to both categories. If they are only tagged to one of the two categories. They won’t show.

    The site I linked to above shows the parameter to query_posts() that you need to use to select posts that are in two categories. If you are using query_posts() in your template, that should be all you need. If not, I will need to see some of the code from your template to provide further help. About 5 or so lines before and after the loop should do.

    To display posts that appear in multiple categories you can use the category__and parameter. The code here will display posts that appear in category 3, category 8 AND category 9. You can add as many as you like to this list.
    query_posts(array(“category__and” => array(2,8,9)));

    If that solves your problem, please mark this topic ‘Resolved’.

    Thread Starter gbrown88

    (@gbrown88)

    Tried working that into my code, this is what i have so far:

    <?php $recent = new WP_Query('category_and=>array('.$meta_cat_id.',7)&showposts=5'); while($recent->have_posts()) : $recent->the_post();?>

    This is on a page template and I have the $meta_cat_id saved from a Custom Field earlier in the file.

    Try this:

    $args = array(
        "category__and" => array($meta_cat_id,7),
        "showposts" => 1,
    );
    <?php $recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>
    Thread Starter gbrown88

    (@gbrown88)

    Worked perfect! Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘List Posts if Post is in Both Category A & B’ is closed to new replies.