Show post only if match all categories

Question

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();?>
0
Tanvir 3 months 0 Answers 16 views 0

Leave an answer