按ID获取术语,ID顺序为

时间:2017-02-02 作者:Arif Khan

我想通过ID和ID顺序获取术语。但这不起作用,WP会自动更改顺序。

我的代码-

$catsArray = array(159, 155, 143, 153, ......);
$series = get_terms( array(
              \'taxonomy\' => \'ctc_sermon_series\',
              \'number\'  =>  9,
              \'offset\'  =>  $offset,
              \'include\' => $catsArray,
              \'hide_empty\'  => false, 
            ) );
结果呢-

(
    [0] => WP_Term Object
        (
            [term_id] => 155
            [name] => 10
            [slug] => 10
            [term_group] => 0
            [term_taxonomy_id] => 155
            [taxonomy] => ctc_sermon_series
            [description] => 
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

    [1] => WP_Term Object
        (
            [term_id] => 159
            [name] => 14
            [slug] => 14
            [term_group] => 0
            [term_taxonomy_id] => 159
            [taxonomy] => ctc_sermon_series
            [description] => 
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

    [2] => WP_Term Object
        (
            [term_id] => 153
            [name] => Name 8
            [slug] => name-8
            [term_group] => 0
            [term_taxonomy_id] => 153
            [taxonomy] => ctc_sermon_series
            [description] => Name 8 Des
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

    [3] => WP_Term Object
        (
            [term_id] => 143
            [name] => Series 1
            [slug] => series-1
            [term_group] => 0
            [term_taxonomy_id] => 143
            [taxonomy] => ctc_sermon_series
            [description] => Series 1 Description
            [parent] => 0
            [count] => 3
            [filter] => raw
        )

)
我希望首先得到ID-159的term结果,但每次我都首先得到ID-155的结果。我需要通过ID的数组序列获得结果。

提前感谢:)

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

因此,我认为问题是如何按照您提供的ID的顺序获取术语-可能不是升序或降序,而是随机顺序。

令人惊讶的是,我认为WP中有一条捷径——谁知道呢?我相信这就是你想要使用的:

$catsArray = array(159, 155, 143, 153, ......);
$series = get_terms( array(
          \'taxonomy\' => \'ctc_sermon_series\',
          \'number\'  =>  9,
          \'offset\'  =>  $offset,
          \'include\' => $catsArray,
          \'hide_empty\'  => false, 
          \'orderby\'  => \'include\', // <--- 
        ) );
希望这有帮助!

SO网友:Anwer AR

这可能对你有用,虽然我没有测试过。

$catsArray = array(159, 155, 143, 153, ......);
$series = get_terms( array(
              \'taxonomy\' => \'ctc_sermon_series\',
              \'number\'  =>  9,
              \'offset\'  =>  $offset,
              \'include\' => $catsArray,
              \'hide_empty\'  => false, 
              \'orderby\' => \'term_id\',
              \'order\' => \'DESC\', // or ASC
            ) );

相关推荐

如何读取WordPress$Query关联数组(散列)键的值

WordPress编程新手(来自更为传统的环境),并试图了解其一些“独特”特性。我们的网站上有一个目录页,此代码驻留在functions.php, 如果条件为true,则调整结果。if( $query->is_post_type_archive( \'directory\' ) ){ ...//do stuff } 我想知道如何获取is_post_type_archive 这就是“目录”当我对值使用测试时。。。var_dumb($query->is_post