显示使用自定义分类的所有帖子

时间:2013-10-23 作者:Josh

我试图显示所有使用自定义分类法的帖子。我认为问题在于,我的代码没有生成术语数组。

 $clients_terms = get_terms(\'clients\');

    $clients_query = new WP_Query( array(
      \'post_type\' => \'work\',
      \'posts_per_page\' => 4,
      \'tax_query\' => array(
        array(
          \'taxonomy\' => \'clients\',
          \'field\' => \'slug\',
          \'terms\' => array($clients_terms->slug)
        )
      )
    ) );
    if ( $clients_query->have_posts() ): ?>
    <ul class="work_list">
      <?php while ( $clients_query->have_posts() ) : $clients_querys->the_post(); ?>
      <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>   </li>
      <?php endwhile; wp_reset_postdata(); ?>
    </ul>
我可以为单个术语生成帖子,但不能为所有使用分类法的帖子生成帖子。

2 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

我认为你的问题在于:

\'terms\' => array($clients_terms->slug)
您定义$clients_terms 像这样:

$clients_terms = get_terms(\'clients\');
。。。它将返回一个对象数组。但您尝试在查询参数中作为对象访问它。这应该会产生某种PHP通知?(试图获取非对象的属性,还是什么?)

尝试构建阵列,然后将其传递给\'terms\':

// Get the array of objects
$clients_terms_objs = get_terms( \'clients\' );

// Define terms array
$clients_terms = array();

// Step through array of objects,
// and populate terms array
foreach ( $clients_terms_objs as $clients_term_obj ) {
    $clients_terms[] = $clients_term_obj->slug;
}

// Construct query
$clients_query = new WP_Query( array(
  \'post_type\' => \'work\',
  \'posts_per_page\' => 4,
  \'tax_query\' => array(
       array(
          \'taxonomy\' => \'clients\',
          \'field\' => \'slug\',
          \'terms\' => $clients_terms
       )
    )
) );

SO网友:Chris_O

这里有一个使用过滤器的替代解决方案,它不需要get\\u terms()调用

/**
 * Extends WP_Query with a posts_join filter allowing you to query by taxonomy instead of tax_query using terms
 * Usage: <code> $query = new Query_By_Taxonomy( array( \'posts_per_page\' => $foo, \'orderby\' => $bar ) ); </code>
 *
 * @class Query_By_Taxonomy
 */
class Query_By_Taxonomy extends WP_Query {

    var $posts_by_taxonomy;
    var $taxonomy;

    function __construct( $args = array() ) {
        add_filter( \'posts_join\', array( $this, \'posts_join\' ), 10, 2 );
        $this->posts_by_taxonomy = true;
        $this->taxonomy = $args[\'taxonomy\'];

        unset( $args[\'taxonomy\'] );

        parent::query($args);
    }

    function posts_join( $join, $query ) {
        if ( isset( $query->posts_by_taxonomy ) && false !== $query->posts_by_taxonomy ) {
            global $wpdb;
            $join .= $wpdb->prepare(
                 "INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID
                  INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id
                  AND {$wpdb->term_taxonomy}.taxonomy=%s",
                $this->taxonomy );
        }
        return $join;
    }
}
用法:
$clients_query = new Query_By_Taxonomy(
    array(
      \'post_type\' => \'work\',
      \'posts_per_page\' => 4,
      \'taxonomy\' => \'clients\',
  )
);

结束

相关推荐

Exclude page name from loop

我当前正在使用此循环:if (is_single() || is_page() ) : if (have_posts() ) : while (have_posts() ) : the_post(); 如何在循环中排除多个页面例如,我想排除页面example.com/music 和example.com/contact.编辑:我已经按照通用汽车提供的方式做了,但我的音乐和联系人页面在申请后一直在加载。<?php if (is_single() |