WordPress sort order by ACF text field

I have an issue that is doing my head in, I’m trying to sort my posts using pre_get_posts by and ACF field that is a text field.

Here is my code:

function my_pre_get_posts( $query ) {

    // do not modify queries in the admin
    if( is_admin() ) {

        return $query;

    }

    if( $query->is_main_query() && $query->is_tax('locations')) {

        $query->set('meta_key', 'level');
        $query->set('orderby', 'meta_value');
        $query->set('order', 'DESC');

    }   

    // return
    return $query;

}

add_action('pre_get_posts', 'my_pre_get_posts');

In the ACF config, it’s set to text and the values of this text can be Bronze, Silver, Gold etc.

When I run the below nothing gets returned.

Any ideas?

Read more here: WordPress sort order by ACF text field

Leave a Reply

Your email address will not be published. Required fields are marked *