admin管理员组

文章数量:1298180

We created a custom posttype called 'Handbook' and it has its own categories. Inside this handbook-category we have categories like 'fase 1', 'fase 2' etc. All the posts in this posttype got one of these categories (fase 1 etc.) selected. On the al the posts in this posttype I need a section with related posts with the same category.

For this I used the following code;

function ms_related_kb() {

    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category( $post_id );

    if(!empty($categories) && is_wp_error($categories)):
        foreach ($categories as $category):
            array_push($cat_ids, $category->term_id);
        endforeach;
    endif;

    $current_post_type = get_post_type($post_id);
    $query_args = array( 

        'category__in'   => $cat_ids,
        'post_type'      => $current_post_type,
        'post__not_in'    => array($post_id),
        'posts_per_page'  => '5'

     );

    $related_cats_post = new WP_Query( $query_args );
    

    if($related_cats_post->have_posts()):
         while($related_cats_post->have_posts()): $related_cats_post->the_post();
            $postIcon = get_field('artikel_icon');
            $postsList .= '<li class="kb-related-item"><a href="' . get_the_permalink() . '" class="kb-related-link"><div class="kb-related-box"><img src="' . $postIcon[url] . '" alt="ALT" class="kb-related-image"><p class="kb-related-title">' . get_the_title() . '</p></div></a></li>';
        endwhile;
    
        return '<ul class="kb-related-list">' . $postsList . '</ul>';

        // Restore original Post Data
        wp_reset_postdata();
     endif;
}
add_shortcode('ms_related_kennisartikelen', 'ms_related_kb');

The problem is that with this code I get all the related posts of the complet posttype 'Handbook'. What do I need to change to get all the related posts of the categories and not the posttype?

We created a custom posttype called 'Handbook' and it has its own categories. Inside this handbook-category we have categories like 'fase 1', 'fase 2' etc. All the posts in this posttype got one of these categories (fase 1 etc.) selected. On the al the posts in this posttype I need a section with related posts with the same category.

For this I used the following code;

function ms_related_kb() {

    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category( $post_id );

    if(!empty($categories) && is_wp_error($categories)):
        foreach ($categories as $category):
            array_push($cat_ids, $category->term_id);
        endforeach;
    endif;

    $current_post_type = get_post_type($post_id);
    $query_args = array( 

        'category__in'   => $cat_ids,
        'post_type'      => $current_post_type,
        'post__not_in'    => array($post_id),
        'posts_per_page'  => '5'

     );

    $related_cats_post = new WP_Query( $query_args );
    

    if($related_cats_post->have_posts()):
         while($related_cats_post->have_posts()): $related_cats_post->the_post();
            $postIcon = get_field('artikel_icon');
            $postsList .= '<li class="kb-related-item"><a href="' . get_the_permalink() . '" class="kb-related-link"><div class="kb-related-box"><img src="' . $postIcon[url] . '" alt="ALT" class="kb-related-image"><p class="kb-related-title">' . get_the_title() . '</p></div></a></li>';
        endwhile;
    
        return '<ul class="kb-related-list">' . $postsList . '</ul>';

        // Restore original Post Data
        wp_reset_postdata();
     endif;
}
add_shortcode('ms_related_kennisartikelen', 'ms_related_kb');

The problem is that with this code I get all the related posts of the complet posttype 'Handbook'. What do I need to change to get all the related posts of the categories and not the posttype?

Share Improve this question edited Sep 17, 2021 at 13:31 Ricardio asked Sep 17, 2021 at 13:10 RicardioRicardio 381 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

@ricardio-de-penning I think you are going on wrong direction. You just want to get only those posts which is related to current category right?

If yes then you need to filter post using tax_query like below.

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array($cat_ids),
            'operator ' => 'IN',
        ),
    ),
);
$query = new WP_Query( $args );

本文标签: categoriesHow to show related posts of category on post within custom posttype