handling localisation meta query

This commit is contained in:
Antoine M 2024-11-13 13:52:49 +01:00
parent 655ff4bc73
commit 7c87c99fd5

View File

@ -22,11 +22,11 @@ add_action('rest_api_init', function () {
function build_search_artisan_posts_cards($request)
{
// write_log($request);
$currentLanguage = esc_html($request->get_param('current-page-language')) ?? 'fr';
$previousActivePage = esc_html($request->get_param('active-page')) ?? 1;
$postsPerPage = esc_html($request->get_param('posts-per-page')) ?? 12;
$taxonomy = esc_html($request->get_param('taxonomy')) ?? 'metiers';
$localisation = esc_html($request->get_param('localisation')) ?? null;
$StringifiedTaxonomyIds = esc_html($request->get_param('taxonomy-ids')) ?? null;
$taxonomyIds = explode(',', $StringifiedTaxonomyIds);
@ -42,6 +42,16 @@ function build_search_artisan_posts_cards($request)
'field' => 'term_id',
)
);
$metaQuery = [];
if ($localisation && $localisation !== 'all') {
$metaQuery[] = array(
'key' => 'state',
'value' => $localisation,
'compare' => '='
);
// write_log($metaQuery);
}
do_action('wpml_switch_language', $currentLanguage);
@ -52,15 +62,17 @@ function build_search_artisan_posts_cards($request)
"posts_per_page" => -1,
"paged" => $activePage,
"tax_query" => $taxQuery,
// "meta_query" => [],
"meta_query" => $metaQuery,
);
$newsPostsDatas = new WP_Query($args);
ob_start();
foreach ($newsPostsDatas->posts as $key => $post) {
// continue en fonction
$post_thumbnail = get_the_post_thumbnail($post->ID, 'full', array('class' => 'card-artisans__thumbnail')) ?? null;
$news_type = get_the_terms($post->ID, "news_type") ?? null;
$post_date = get_the_date('j.m.Y', $post->ID) ?? null;
@ -76,7 +88,6 @@ function build_search_artisan_posts_cards($request)
'news_type' => $news_type,
'post_date' => $post_date,
'current_taxonomy' => $taxonomy,
)
);
}
@ -97,3 +108,33 @@ function build_search_artisan_posts_cards($request)
return $response;
}
function debugQueryTest()
{
$taxQuery = array(
'taxonomy' => 'metiers',
'terms' => array(17, 18, 19, 20),
'field' => 'term_id'
);
$metaQuery = array(
array(
'key' => 'state',
'value' => "brussels",
'compare' => '='
)
);
$args = array(
"status" => "publish",
"post_type" => "artisans",
"posts_per_page" => -1,
"tax_query" => $taxQuery,
"meta_query" => $metaQuery,
);
$newsPostsDatas = new WP_Query($args);
}
// debugQueryTest();