2

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();?>
2
  • 2
    Can you indent your code? It's difficult to read ( also wp_query needs to be WP_Query ) – Tom J Nowell Oct 29 '20 at 21:33
  • 1
    Indented, hope it's easy to read now. – Tanvir Oct 29 '20 at 22:16
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.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.