Skip to content

Instantly share code, notes, and snippets.

@cubehrends
Created April 15, 2024 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cubehrends/422466da662152aad0f3e753282f7e24 to your computer and use it in GitHub Desktop.
Save cubehrends/422466da662152aad0f3e753282f7e24 to your computer and use it in GitHub Desktop.
Pre Get Post CPT Partner
<?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