Skip to content

Instantly share code, notes, and snippets.

@guytzhak
Created July 6, 2021 08:47
Show Gist options
  • Save guytzhak/f35943e6e698dc1ea471e7a0a4878b55 to your computer and use it in GitHub Desktop.
Save guytzhak/f35943e6e698dc1ea471e7a0a4878b55 to your computer and use it in GitHub Desktop.
function filter_clinics_index( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'clinic' )){
$meta_query = $query->get('meta_query');
if( is_array($meta_query) ) {
$meta_query['order_clinic_clause'] = [
'key' => 'order_clinic',
'compare' => 'EXISTS',
'type' => 'NUMERIC'
];
} else {
$meta_query = [
'relation' => 'AND',
'order_clinic_clause' => [
'key' => 'order_clinic',
'compare' => 'EXISTS',
'type' => 'NUMERIC'
]
];
}
$order_by = [
'order_clinic_clause' => 'meta_value_num rand',
'rand' => 'DESC'
];
$query->set( 'orderby' , $order_by );
$query->set( 'meta_key' , 'promotion_date_end' );
$query->set( 'meta_query' , $meta_query );
//$query->set( 'order', 'DESC' );
}
}
add_action( 'pre_get_posts', 'filter_clinics_index', 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment