1
votes

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.

1

1 Answers

0
votes

As described in the WP_Query documentation, if you wish to order by two different pieces of postmeta (for example, property_featured first and property_status second), you need to combine and link your meta query to your orderby array using 'named meta queries'.

Below is your code modified :

$args = array(
    'post_type'      => 'property',
    'posts_per_page' => 12,
    'paged'          => $paged,

    'meta_query'     => array(
        'relation' => 'AND',
        'featured_clause' => array(
            'key' => 'property_featured',
            'compare' => 'EXISTS'
        ),
        'status_clause' => array(
            'key' => 'property_status',
            'compare' => 'EXISTS'
        )
    ),

    'orderby'    => array(
        'featured_clause' => 'ASC',
        'status_clause' => 'ASC'
    )

);