Support

Account

Home Forums General Issues Query and Orderby multiple ACF Fields

Helping

Query and Orderby multiple ACF Fields

  • I have a custom post type called “property”.

    Each property has an ACF checkbox fields called “property_status” with the following options:

    rent : For Rent

    rented : Rented

    sale : For Sale

    sold : Sold Homes

    new : Under Construction

    Each property also has a featured listing ACF radio button field called “property_featured” with the following options:

    no : No

    yes : Yes

    I have successfully queried the posts to show up using the following code:

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    $args = array(
        'post_type' => 'property',
        'posts_per_page' => 12,
        'meta_key' => 'property_status',
        'orderby'    => array(
            'property_featured' => 'ASC',
            'property_status' => 'ASC'
        ),
        'order' => 'ASC',
        'paged' => $paged
        );
    $wp_query = new WP_Query( $args );

    I need to order my posts by calling the featured properties with a radio button selection of “yes : Yes”, then display the other posts under “properties” by their ‘property_status’ of rent, rent and new, sale, then sale and new. Some properties can have 2 check boxes selected in the “property_status” field such as a property for rent, but that is all “new” under construction.

    I have tried several different query declarations to try and display these custom posts by the order I need, but have not had any success. The code above is what I am using.

  • In order to order by multiple meta values you need to use clauses. Information on this is not given in the main WP_Query doc but can be found here https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

    basically you need to replace 'meta_key' => 'property_status' with a full meta_query and include all of the field names you want to order by in it. These meta queries can be all simple “EXIST” queries if your only goal is to order by the fields.

    your “orderby” array would then be similar to what you have but use the clause index for each of the fields instead of the field name.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Query and Orderby multiple ACF Fields’ is closed to new replies.