WordPress 通过自定义分类法获取自定义文章类型的相关文章

有时候我们需要为网站添加一个自定义文章类型,但是当访问此文章类型页面时,如何获取相关联的文章呢?请看以下代码:

$terms = get_the_terms($post->ID, 'product_tags', 'string');
$term_ids = wp_list_pluck($terms, 'term_id');
$second_query = new WP_Query(array(
    'post_type' => 'products',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_tags',
            'field' => 'id',
            'terms' => $term_ids,
            'operator' => 'IN' //Or 'AND' or 'NOT IN'
        )
    ),
    'posts_per_page' => 3,
    'ignore_sticky_posts' => 1,
    'orderby' => 'rand',
    'post__not_in' => array($post->ID)
));
if ($second_query->have_posts()) {
    while ($second_query->have_posts()) : $second_query->the_post();
    //loop
    endwhile;
    wp_reset_query();
}
链接到文章: https://vpsum.com/47406.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注