Question

I try to create blog page with specific categories (by ID) and display only newest blog post from this category. I have code like this but it shows only first category. I have work code for display only one category but when I put more id it doesn't work

<?php
/**
 * Template Name: Porftfolio
 */
 
get_header(); ?>
 
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
 
    <?php
    
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'cat'   => '187,186',
        'posts_per_page' => 1,
    );
    $arr_posts = new WP_Query( $args );
 
    if ( $arr_posts->have_posts() ) :
 
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php
                if ( has_post_thumbnail() ) :
                    the_post_thumbnail();
                endif;
                ?>
                <header class="entry-header">
                    <h1 class="entry-title"><?php the_title(); ?></h1>
                </header>
                <div class="entry-content">
                   <a href> <?php the_permalink(); ?> </a>
                </div>
            </article>
            <?php
        endwhile;
    endif;
    ?>
 
    </main><!-- .site-main -->
</div><!-- .content-area -->
 
<?php get_footer(); ?>
Was it helpful?

Solution

Please add following code

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category__and'   => array( 2, 6 ),
    'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );

OR

$tax_query = array(
relation => 'AND',
array(
    'taxonomy'          => 'category',
    'field'             => 'term_id',   // 'term_id' by default, so just here as an example
    'terms'             => 186,
    'include_children'  => false,       // true by defualt
    'operator'          => 'IN'         // 'IN' by default, so just here as an example
),
array(
    'taxonomy'          => 'category',
    'terms'             => 187,
    'include_children'  => false,       // true by defualt
)

)

OTHER TIPS

If what you want is to get a post that belongs to BOTH categories, you need to use category__in or category__and parameter instead of cat.

You could also use 'category_name' => "first-name+second-name" too, but that takes in the category slugs, not IDs.

Note: Just FYI, that query is fetching 1 post with EITHER of those categories. Not necessarily BOTH at the same time.

For you fetch a post with both categories assigned to it category__in and category__and would do the trick. Difference being that category__in does not show posts in child categories of the ones passed in, while category__and would show child categories too (if exist)

See Category Parameters for more info

P.S: nikhil hadvani's answer seems right as well, just saw it updated as I sent this. Either those or these should fix it.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top