Created
April 15, 2024 09:07
-
-
Save cubehrends/422466da662152aad0f3e753282f7e24 to your computer and use it in GitHub Desktop.
Pre Get Post CPT Partner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function lp_partner_sort( $query ) { | |
if( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive('partner') ){ | |
if ( isset( $_COOKIE['region'] ) && $_COOKIE['region'] !== '0' ) { | |
// Get the value of the "region" cookie | |
$region = $_COOKIE['region']; | |
// Set the query parameters to filter partners by the corresponding taxonomy term | |
$tax_query = array( | |
array( | |
'taxonomy' => 'region', // Replace 'partner_region' with your actual taxonomy name | |
'field' => 'term_id', | |
'terms' => $region, | |
), | |
); | |
$query->set( 'tax_query', $tax_query ); | |
} | |
// Set the order and orderby parameters to sort by the custom field "tarif" in descending order, and then by date in descending order | |
$query->set( 'orderby', array( 'meta_value_num' => 'DESC', 'modified' => 'DESC' ) ); | |
$query->set( 'meta_key', 'tarif' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'lp_partner_sort' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment