分类术语的自定义顺序

时间:2019-04-03 作者:TheSurfer

我正在查询一个自定义的帖子类型,并按术语显示所有帖子(基本上按类别显示),如下所示,除了某些原因之外,一切都很正常。无论我如何编写tax\\u查询数组,我似乎都无法获得要更改的术语顺序(ASC和DESC不会更改任何内容)。

有人能看出我哪里出了问题吗?


$temp_query = $wp_query;

$custom_terms = get_terms(\'instruction_categories\');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array(
        \'post_type\' => \'instruction-sheets\',
        \'orderby\' => \'name\', // order of the products
        \'order\'  => \'ASC\',
        \'hide_empty\' => 1,
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'instruction_categories\',
                \'field\' => \'slug\',
                \'terms\' => $custom_term->slug,
                \'hide_empty\' => 1,
                \'orderby\' => $custom_term->name,
                \'order\' => \'ASC\', // switching to DESC should reverse order, but doesnt
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo \'Region \'.$custom_term->name.\'\';

        while($loop->have_posts()) : $loop->the_post();

2 个回复
SO网友:TheSurfer

For 一nyone h类一v我ng级 t型h类e s一m级e 我ssue, h类ere\'s h类ow 我 ended up solv我ng级 t型h类我s. 我 我nst型一lled t型h类e <一 h类ref=“”h类t型t型ps://wordpress.org级/plug级我ns/wp-t型erm级-order/“” rel=“”nofollow noreferrer“”>WP Term级 Order plug级我n 一nd t型h类en used t型h类e follow我ng级 code, t型h类e m级一g级我c h类一ppens 我n t型h类e \'orderby\' => \'order\' l我ne wh类ere t型h类e \'order\' 我s be我ng级 pulled from级 t型h类e dr一g级 一nd drop m级enu order funct型我on一l我t型y 我n t型h类e 一dm级我n 一dded by t型h类e plug级我n.

&#x个A.;&#x个A.;

$custom_terms = get_terms(\'instruction_categories\');

foreach($custom_terms as $custom_term) {
wp_reset_query();

$args = array(
    \'post_type\' => \'instruction-sheets\',
    \'orderby\' => \'meta_value\',
    \'order\'  => \'ASC\',
    \'hide_empty\' => 1,
    \'meta_key\' => $custome_term->slug,
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'instruction_categories\',
            \'field\' => \'slug\',
            \'terms\' => $custom_term->slug,
            \'hide_empty\' => 1,
            \'orderby\' => \'order\',
            //  \'order\' => \'DESC\',
        ),
    ),
 );


 $loop = new WP_Query($args);
 if($loop->have_posts()) {

 while($loop->have_posts()) : $loop->the_post();
&#x个A.;&#x个A.;

Th类en ent型er wh类一t型ever code you need t型o sh类ow for p一rt型s of your post型 cont型ent型, 一nd t型h类en don\'t型 forg级et型 t型o close t型h类e wh类ole t型h类我ng级 out型 w我t型h类:

&#x个A.;&#x个A.;

&#x个A.;    endwh类我le; &#x个A.;    }&#x个A.;}&#x个A.;

&#x个A.;

SO网友:jdm2112

我认为你的质疑论点需要修改。的值orderby 应该是meta_valuemeta_value_num 对于数字排序。

您还需要meta_key 在arguments数组中设置,在您的情况下,该值应设置为$custom_term->slug.

查询的未测试参数:

$args = array(
    \'post_type\' => \'instruction-sheets\',
    \'orderby\' => \'meta_value\', // Change #1
    \'order\'  => \'ASC\',
    \'hide_empty\' => 1,
    \'meta_key\' => $custome_term->slug,
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'instruction_categories\',
            \'field\' => \'slug\',
            \'terms\' => $custom_term->slug,
            \'hide_empty\' => 1,
        ),
    ),
 );
用于排序的WP Codex参考:https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

相关推荐