我的一个客户端有自定义的帖子类型,他们使用插件而不是在函数中定义它们。php。(因此,YARPP的解决方案行不通)。在此自定义帖子类型中,它们有各种类别。每个帖子都属于一个或多个类别。在每个帖子之后,他们希望显示所有相同类别的5篇帖子(可以是一个或多个类别)。
所以,我需要做的是设置一种方法来动态查找一篇文章所属的所有类别(我已经这样做了&在我解决这个问题之前,请立即将其设置为回显),然后查询以查找属于完全相同类别的5篇文章(要么属于所有类别,要么不应显示)。
这是我想到的代码,但我不知道如何“组合它们”,因此它不仅可以获取术语(类别),还可以强制查询使用这些术语来过滤5个“相关”程序。
//get the post\'s terms (troubleshooting - can be removed once figured out!
$category_terms = wp_get_object_terms($post->ID, \'category\');
if(!empty($category_terms)){
if(!is_wp_error( $category_terms )){
echo \'Terms <ul>\';
foreach($category_terms as $term){
echo \'<li><a href="\'.get_term_link($term->slug, \'category\').\'">\'.$term->name.\'</a></li>\';
}
echo \'</ul>\';
}
}
//get post terms done
// get the custom post type\'s taxonomy terms
$custom_taxterms = wp_get_object_terms( $post->ID,
\'category\', array(\'fields\' => \'ids\') );
// arguments
$args = array(
\'post_type\' => \'program_listings\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 5, // you may edit this number
\'orderby\' => \'rand\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'id\',
\'terms\' => $custom_taxterms
)
),
\'post__not_in\' => array ($post->ID),
);
$related_items = new WP_Query( $args );
// loop over query
if ($related_items->have_posts()) :
echo \'<h2>Other Programs in this category</h2><ul>\';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li style="margin-left:10px;list-style:none;"><a href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></li>
<?php
endwhile;
echo \'</ul>\';
endif;
// Reset Post Data
wp_reset_postdata();
因此,获取帖子条款(&P);回显它们(完成后不会是回显-我这样做是为了确保它确实得到了类别/术语)效果很好。它显示帖子所在的每个“类别”。但是,第二位显示来自任何类别的帖子,而不是仅在所有类别中的帖子。基本上,我需要将第一个和;第二部分,以便查询仅返回当前帖子所有类别中的帖子结果。如果某个帖子不在所有相同的类别中(与当前帖子的所有类别完全匹配),则不应返回该帖子。(例如,如果某个帖子属于父级“北美”类别,但不属于该帖子所属的任何其他类别,则不应在结果中返回该帖子)。