From 7c87c99fd5a52123b7391c6de2eea5686acf10e7 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Wed, 13 Nov 2024 13:52:49 +0100 Subject: [PATCH] handling localisation meta query --- includes/api.php | 49 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/includes/api.php b/includes/api.php index dee54a3..a10da49 100644 --- a/includes/api.php +++ b/includes/api.php @@ -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();