I have posts that have multiple different categories including a common category called “Videos”, now I’m trying to show only posts that match with the all categories of the main post including the common “Video” category.

Suppose, the main post has: Catagory A, Catagory B, Videos

The output posts must be matching categories like this:

Catagory A, Catagory B, Videos (Output 1)

Catagory A, Catagory B, Videos (Output 2)

Catagory A, Catagory B, Videos (Output 3)…

With the following code, it’s showing all the posts from “Videos” category:

<?php $orig_post = $post;
    global $post;
    $categories = get_the_category($post->ID);
    if ($categories) {
    $category_ids = array();
    foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    $args=array(
    'post__not_in' => array($post->ID),
    'posts_per_page'=> 30,
    'tax_query' => array( array(
        'taxonomy' => 'post_format',
        'field' => 'slug',
        'terms' => array('post-format-video'),
        'operator' => 'IN'
       ) )
    );
    $my_query = new WP_Query( $args );
    if( $my_query->have_posts() ) {?>
<div class="sidebar">
    <?php while( $my_query->have_posts() ) { $my_query->the_post();?>

1 Answer
1

Here’s an updated version of your code that will get posts that have the Post Format Video and that have the same Categories as the current post in the loop.

You’ll call wpse_get_video_posts_with_matching_categories() to execute the function.

function wpse_get_video_posts_with_matching_categories() {
    $categories = get_the_category( get_the_ID() );

    if ( empty ( $categories ) ) {
        return;
    }

    $my_query = new WP_Query( [
        'post__not_in'   => [ get_the_ID() ],
        'posts_per_page' => 30,
        'tax_query'      => [
            'relation' => 'AND', // We will check BOTH of the following criteria.
            [
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => [ 'post-format-video' ],
                'operator' => 'IN',
            ],
            [
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => wp_list_pluck( $categories, 'term_id' ), // A handy way to extract data.
                'operator' => 'AND',
            ],
        ],
    ] );
    ?>

    <?php if ( $my_query->have_posts() ) : ?>

    <div class="sidebar">
        <?php while( $my_query->have_posts() ) : ?>
            <?php
                $my_query->the_post();
                // Output content...
                the_title( '<h3>', '</h3>' );
            ?>
        <?php endwhile; ?>
    </div>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    <?php
}

I tested this and I think it’s what you’re after. Note that your original code is missing the part where we query against the term IDs. I also spruced things up a bit with some best practices.

Leave a Reply

Your email address will not be published. Required fields are marked *