Sort by an ACF field in a tax_query

I do not find the solution which must surely be surely simple. (I'm new to WP) I have an ACF field named "partners_order". I would like to sort it in ascending order (ASC)

here's what i tried

?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',
        ),
    ),

) ); ?

if someone could give me a lead to move forward ... thank you

Topic tax-query order custom-field Wordpress

Category Web


Thank you for your answer, but when I test nothing displays except the type_partenaires.

I think I was wrong on the explanation of my problem. sorry.

I would like to be able to sort by the "partners_order" field. this field is in my while after my $loops

<?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>

You have placed orderby in a wrong array. Please check modified code.

$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

        ),
    ),

) ); 

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.