在自定义Wordpress查询中获取所有术语


Get all terms in a custom Wordpress Query?

我知道如何获得所有的Wordpress术语,但我需要一个"过滤"的结果版本。是否有可能得到Wordpress查询结果中的所有术语?我在这里有这样的查询:

<?php
   $args=array(
  'post_type' => 'gw_activity',
  'post_status' => 'publish',
  'orderby' => 'date',
  'meta_query' => array(
     'relation' => 'AND',
      array(
         'key' => 'activity_category',
         'value' => 'mindful_life',
         'compare' => '='
      )
   ), 
  'posts_per_page' => 10
);
$my_query = null; 
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { 
 $all_terms = array();
  while ($my_query->have_posts()) : $my_query->the_post(); ?>      
  <?php $terms = wp_get_post_terms( $my_query->post->ID, array( 'gw_activity_tag' ) ); ?>
  <?php       
        foreach ( $terms as $term ) {
            $all_terms[] = $term->name;
        }        
  ?>                                            
  <?php endwhile; }
  wp_reset_query();
?>
<!-- End Custom Query -->
<?php
    $unique_terms = array_unique( $all_terms ); 
    $result = array_unique($unique_terms);
    foreach ($result as $value) {
        echo $value . '<br />';
    }

?>

但是我不知道如何运行查询&在它里面放一个"Where"子句,就像MySQL一样。任何帮助/建议,甚至指给我正确的方向,我将不胜感激。我卡住了

查看此函数:wp_get_post_terms()

假设你的帖子支持tax_atax_b两种分类法,你可以尝试这样做,就在你写评论的地方:

<?php $terms = wp_get_post_terms( $query->post->ID, array( 'tax_a', 'tax_b' ) ); ?>
<?php foreach ( $terms as $term ) : ?>
    <p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>

这将打印查询检索到的每个帖子的所有术语。


编辑

如果您想要的是查询检索到的所有帖子中的所有术语,您可以将值存储在数组中,然后使用像array_unique()这样的函数,如下所示:

$all_terms = array();
foreach ( $terms as $term ) {
    $all_terms[] = $term->name;
}
// ... and outside the WHILE loop
$result = array_unique( $all_terms );
foreach ( $result as $term ) {
    echo $term . '<br/>;
}