最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Show post only if match all categories

programmeradmin1浏览0评论

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();?>

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();?>
Share Improve this question edited Oct 29, 2020 at 22:09 Tanvir asked Oct 29, 2020 at 20:57 TanvirTanvir 213 bronze badges 2
  • 2 Can you indent your code? It's difficult to read ( also wp_query needs to be WP_Query ) – Tom J Nowell Commented Oct 29, 2020 at 21:33
  • 1 Indented, hope it's easy to read now. – Tanvir Commented Oct 29, 2020 at 22:16
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论