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:
- by the ACF field: ccaa_mov_vecinal (this is a text field)
- by the ACF true/false field: destacado_mov_vecinal
- 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') )
Thanks!