在TAX_QUERY中按ACF字段排序

时间:2019-12-31 作者:PIxel up

我找不到一个肯定很简单的解决办法。(我是WP的新手)我有一个名为“partners\\u order”的ACF字段。我想按升序排序(ASC)

这是我试过的

<?php $terms = get_terms(\'type_partenaires\');

usort($terms, function($a, $b) {
    return get_field(\'ordre_affichage\', $a) - get_field(\'ordre_affichage\', $b);
});

foreach ( $terms as $term ): ?>

    <?php echo $term->name; ?>


<?php $loop = new WP_Query( array(
    \'post_type\' => \'partenaires\',
    \'orderby\'  => \'title\',
    \'order\'    => \'ASC\',
    \'posts_per_page\' => -1,
    \'paged\' => $paged,
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'type_partenaires\',
            \'field\'    => \'ID\',
            \'terms\'    => $term->term_id,
            \'orderby\'  => \'partners_order\',
            \'order\'    => \'ASC\',
        ),
    ),

) ); ?>
如果有人能给我一个前进的线索。。。非常感谢。

2 个回复
SO网友:Pradipta Sarkar

您将orderby放置在错误的数组中。请检查修改后的代码。

$loop = new WP_Query( array(
    \'post_type\' => \'partenaires\',
    \'orderby\'  => \'title\',
    \'order\'    => \'ASC\',
    \'posts_per_page\' => -1,
    \'paged\' => $paged,
   \'meta_key\'=> \'partners_order\', 
   \'orderby\' => \'meta_value_num\', 
   \'order\' => \'ASC\', 
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'type_partenaires\',
            \'field\'    => \'ID\',
            \'terms\'    => $term->term_id

        ),
    ),

) ); 

SO网友:PIxel up

谢谢你的回答,但当我测试时,除了type\\u partenaires之外,什么都不会显示。

我想我对我的问题的解释是错误的。很抱歉

我希望能够按“partners\\u order”字段进行排序。此字段位于$循环之后的my while中

<?php $terms = get_terms(\'type_partenaires\');

                        usort($terms, function($a, $b) {
                            return get_field(\'ordre_affichage\', $a) - get_field(\'ordre_affichage\', $b);
                        });


                        foreach ( $terms as $term ): ?>

                            <div class="cell small-12 medium-12 large-12">
                                <h2 class="title-type-partenaire"><?php echo $term->name; ?></h2> 
                            </div>  

                            <?php 

                                $loop = new WP_Query( array(
                                \'post_type\' => \'partenaires\',
                                \'orderby\'  => \'title\',
                                \'order\'    => \'ASC\',
                                \'posts_per_page\' => -1,
                                \'paged\' => $paged,
                                \'tax_query\' => array(
                                    array(
                                        \'taxonomy\' => \'type_partenaires\',
                                        \'field\'    => \'ID\',
                                        \'terms\'    => $term->term_id,

                                    ),
                                ),


                                ) ); ?>

                        <div class="cell large-12">
                            <ul class="flex-partenaire">
                            <?php if (have_posts()) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

                                <?php get_template_part( \'template-parts/loop\', \'page-partenaires\' ); ?>

                            <?php endwhile; ?>
                            </ul>   
                        </div>

相关推荐

干扰Orderby相关性的元查询

我正在尝试使用pre_get_posts 钩住并设置orderby 相关参数。我还想把优先帖子,这是基于帖子元_prioritize_s 在结果中放在第一位。之后的所有帖子都会像往常一样以相关性为基础。以下是我一直在处理的代码://runs on pre_get_posts function modify_search_results_order( $query ) { if ( $query->is_main_query() && is_search