inner foreach loop sorting

时间:2018-01-21 作者:shafshaf

我有以下脚本,需要按字母顺序对内部foreach循环进行排序,但没有成功。

$args = array( 
        \'post_type\' => \'clinical_sites\',
        \'tax_query\' => array( 
                    \'relation\' => \'AND\',
                    array(
                            \'taxonomy\' => \'research\', 
                            \'field\' => \'slug\',
                            \'operator\' => \'AND\',
                            \'hide_empty\' => false,
                            \'terms\' => $researchName
                    ),
                    array( 
                            \'taxonomy\' => \'country\',
                            \'hide_empty\' => false,
                            \'operator\' => \'IN\',
                            \'field\'    => \'term_id\',
                            \'terms\' => $fatherTermId 
                        ),
                    ),
        \'hide_empty\' => true,
        \'posts_per_page\'=>-1,
        \'parent\' => \'1\'
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while( $query->have_posts() ) { $query->the_post();

            $args3 = array( 
                \'orderby\'  => \'ID\',
                \'order\'    => \'DESC\',
                \'childless\' => true
            );
            $terms = wp_get_object_terms( $post->ID, \'country\', $args3  );
            if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
                foreach ( $terms as $parent_term ) {


                        ?><option value="<?=$parent_term->name; ?>" data-is-parent="<?=$isparent?>" data-term_id="<?=$parent_term->term_id; ?>" ><?php echo $parent_term->name?></option><?php
                } 
            }
        }
    }
我们将不胜感激。

1 个回复
SO网友:swissspidy

看起来你只需要换个脚趾orderby 你的论点wp_get_object_terms() 呼叫毕竟,这就是你在更深层循环的东西。

如果你改变\'orderby\' => \'ID\'\'orderby\' => \'name\', 您的条款将按字母顺序排列。

结束

相关推荐

Sort users by meta_value_num

我在使用WP_User_Query 要按元数值对用户进行排序,我认为这很简单,因为它只按字母降序显示结果。 <?php $args = array( \'orderby\' => \'meta_value_num\', \'meta_key \' => \'epicredvote\', \'order\' => \'DESC\', ); // The Query $user_query = n