Forums

[resolved] Only showing posts that is in both in categories X & Y (3 posts)

  1. newbreed65
    Member
    Posted 10 months ago #

    Hi

    I am currently trying to work out the best way to only show posts which are in 2 specific categories, so post's that are in category X and in category Y. I have currently done it by the below code but it isn't an great solution since I will end up with less posts than what my limit is set at.

    query_posts( 'posts_per_page=10&cat=10,15' );                
    
     while ( have_posts() ) : the_post();
    
         if ( in_category(15) &&  in_category(10)) {
    
    ?><li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?></a></li><? 
    
         } 
    
    endwhile;
    
    wp_reset_query();

    Anyone got any suggestions? or How would I manage it via a MySQL query?

  2. keesiemeijer
    moderator
    Posted 10 months ago #

    try it with:

    $args = array(
      'category__and' => array( 10,15 ),
      'posts_per_page => 10
     );
    query_posts( $args );
  3. newbreed65
    Member
    Posted 10 months ago #

    Awesome, Thank you!

    I actually managed to find the solution below but your version is better

    http://wordpress.org/support/topic/custom-query-for-multiple-categories?replies=7

Reply

You must log in to post.

About this Topic