I am using the following code to modify the CPT "movimiento" query.

add_action( 'pre_get_posts', 'mov_vec_archive_page' );

function mov_vec_archive_page( $query ) {
    if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'movimiento' ) || !is_admin() && $query->is_main_query() && is_tax('ccaa') ) {
            $query->set( 'posts_per_page', '8' );       
            $meta_query = array(
                'relation' => 'OR',
                'ccaa_order' => array(
                    'key' => 'ccaa_mov_vecinal',
                'featured_order' => array(
                    'key' => 'destacado_mov_vecinal',
            $order_by = array(
                'ccaa_order' => 'ASC',
                'featured_order' => 'ASC',
        $query->set( 'meta_query', $meta_query );
        $query->set( 'orderby', array($order_by, 'title' => 'ASC') );
    // return
    return $query;

It is a list that I want to sort by 3 fields as follows:

  1. by the ACF field: ccaa_mov_vecinal (this is a text field)
  2. by the ACF true/false field: destacado_mov_vecinal
  3. by entry title

I have managed to get it to sort via field 1 and 3 but 2 is giving me problems because the ACF true/false field content can be one of these 3 values:

  • 1 for true
  • 0 for false
  • empty

Most entries will have the value empty (some will have value 0) and only the ones I check will have the value 1

The order should be: First, alphabetical order using the ACF field: ccaa_mov_vecinal. If several entries have the same value of that field, the first of all them will be the one with the value 1 in the ACF true/false field and then the rest (with 0 or empty value in the true/false field) in alphabetical order.

If you can't help me with this question, I would also appreciate a review of the code used to modify the query and if I'm messing up on something I don't know.

Especially with the part I use to apply the query modification for the CPT movement and the ccaa taxonomy, to know if this is the proper way to do it:

if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'movimiento' ) || !is_admin() && $query->is_main_query() && is_tax('ccaa') )


  • Maybe this could help? stackoverflow.com/questions/56714587/… If not, I think you can easily handle this via PHP as well, if in case the query becomes slower because of the sorting.
    – Kumar
    Nov 3, 2022 at 6:59
  • Thanks for your suggestion @kumar. I've tried to apply it but didn't work. I checked a lot of similar solutions that I found in this forum and none of them worked for me. I have contacted a developer who may be able to fix it for me. I will leave the answer here as soon as I get it.
    – xaifu
    Nov 3, 2022 at 19:11

1 Answer 1


I have found the solution to my question and I share it. I hope someone else will find it useful in the future.

I am not a developer and surely the code can be improved, but this one worked for me for what I wanted.

I needed to show a list of posts ordered by the following parameters:

First, show the posts grouped by a custom field: custom_field_to_group_posts. With this, we group all the posts with the same value in that custom field.

Second, within each grouping of posts, the posts should be displayed in alphabetical order.

Finally, if any post is marked as featured using an ACF custom field true/false, that post should be moved to the top of the list within the grouped posts.

To be understood, I want to group the posts by city and within each city the featured post will be the first of that city.

  • post_A cityA

  • post_B cityA

  • post_C cityA

  • post_D cityB FEATURED

  • post_A cityB

  • post_B cityB

  • post_C cityB

  • post_C cityC FEATURED

  • post_A cityC

  • post_B cityC

  • ...

This is the code I used and it worked for me.

add_action( 'pre_get_posts', 'my_custom_query' );

function my_custom_query( $query ) {
     // Do not add meta arguments in admin pages
    if (is_admin()) {

    if( $query->is_main_query() && is_post_type_archive( 'my_CPT_name' ) {
        //Get original meta query
        $meta_query = (array)$query->get('meta_query');

        // Add your criteria
        $query->set( 'posts_per_page', '8' );
        $meta_query = array(
            'custom_field_to_group_posts' => array(
                'key' => 'custom_field_to_group_posts', // The name of my custom field
            'featured_my_cpt' => array(
                'key' => 'featured_my_cpt', // The name of the true/false ACF field

        // Set the meta query to the complete, altered query
        $query->set( 'meta_query', $meta_query );
        $query->set( 'orderby', array( 'custom_field_to_group_posts' => 'ASC', 'featured_my_cpt' => 'DESC', 'title' => 'ASC'  ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.