Sort post by acf date first and after by title

Viewed 20

I have a small issue on a website.

I need to display movies (my custom post: ‘films’) and some have a release date (ACF field : ‘date_de_sortie’) and others do not have one.

I would like to display in first movies with release date (from the the closer to the further ) and only after, those which don’t have a release date (ordered by title).

For now it displays in first movies without a release date in and after those with a release date, order is ok. Here is my function :

add_action( 'pre_get_posts', 'films_query' );
function films_query( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'films' ) ) {
$cat_hide = get_field('categorie_des_films_a_masquer', 'options');
$taxquery = array(
array(
'taxonomy' => 'categorie_films',
'field' => 'term_id',
'terms'    => $cat_hide,
'operator' => 'NOT IN',
)
);
$query->set( 'posts_per_page', '-1' );
$query->set( 'post_status', 'publish' );

$query->set( 'meta_key', 'date_de_sortie' );
$query->set( 'orderby', 'meta_value title' );
$query->set( 'order', 'ASC' );

$query->set( 'tax_query', $taxquery );
}
}

Do I need to use $query->set( ‘meta_query’, $meta_query ); with $meta_query args ?

Any help appreciated.

All the best.

Pierre

0 Answers
Related