Search code examples
wordpresswhile-loopwordpress-themingcategories

How do I display posts from one category or another in wordpress loop?


I am trying to create a wordpress loop that shows posts from either category 1 or category 2. It doesn't have to be both. What i have at the moment only show posts that are in both categories. I have looked all over the internet but can't find exactly what i am looking for.

This is the code for my loop that is currently only showing posts assigned to both categories.

     <?php 
// Example argument that defines three posts per page. 
$args = array( 
    'posts_per_page' => 3,
    'cat' => 1,
    'cat' => 154
); 
 
// Variable to call WP_Query. 
$the_query = new WP_Query( $args ); 
 
if ( $the_query->have_posts() ) : 
$i = 0;
    // Start the Loop 
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
if ( $i == 0 ) :  ?>


//loop content here


<?php endif; 
    // End the Loop 
$i++;
    endwhile; 
else: 
// If no posts match this query, output this text. 
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); 
endif; 
 
wp_reset_postdata(); 
?>

Solution

  • Have you tried using category__in argument instead?, like this:

        category__in => array(1, 154)