From de37ae1e7d90602403d73ccf3ec8215b10b8466a Mon Sep 17 00:00:00 2001 From: Antoine M Date: Mon, 2 Dec 2024 12:29:28 +0100 Subject: [PATCH] handling search --- includes/api.php | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/includes/api.php b/includes/api.php index ffac30e..0c3ce16 100644 --- a/includes/api.php +++ b/includes/api.php @@ -31,19 +31,20 @@ function build_search_artisan_posts_cards($request) $taxonomyIds = explode(',', $StringifiedTaxonomyIds); $taxonomyIds = array_map('intval', $taxonomyIds); - + $search_value = sanitize_text_field($request->get_param('search')) ?? null; $activePage = is_numeric($previousActivePage) ? $previousActivePage + 1 : 1; - $taxQuery = array( - array( + $taxQuery = []; + if ($taxonomy && !empty($taxonomyIds)) { + $taxQuery[] = array( 'taxonomy' => $taxonomy, 'terms' => $taxonomyIds, 'field' => 'term_id', - ) - ); + ); + } $metaQuery = []; - + if ($localisation && $localisation !== 'all') { $localisationArray = explode(',', $localisation); // Séparer les localisations par des virgules $metaQuery[] = array( @@ -53,8 +54,6 @@ function build_search_artisan_posts_cards($request) ); } - - do_action('wpml_switch_language', $currentLanguage); $args = array( @@ -62,12 +61,13 @@ function build_search_artisan_posts_cards($request) "post_type" => "artisans", "posts_per_page" => -1, "paged" => $activePage, - "tax_query" => $taxQuery, + "tax_query" => $taxQuery, // "meta_query" => [], "meta_query" => $metaQuery, - + 's' => $search_value, ); $newsPostsDatas = new WP_Query($args); + // write_log($newsPostsDatas->posts); ob_start(); @@ -95,6 +95,11 @@ function build_search_artisan_posts_cards($request) $html_template = ob_get_clean(); + + + if ($newsPostsDatas->found_posts === 0) { + $html_template = "

" . __("Aucun résultat trouvé", "metiers-patrimoine-theme") . "

" . __("Essayez d'ajouter un filtre supplémentaire pour trouver plus de résultats", "metiers-patrimoine-theme") . "

"; + } $response_data = array( 'html_template' => $html_template, 'total_posts_found' => $newsPostsDatas->found_posts, @@ -131,9 +136,11 @@ function debugQueryTest() "status" => "publish", "post_type" => "artisans", "posts_per_page" => -1, - "tax_query" => $taxQuery, - "meta_query" => $metaQuery, + // "tax_query" => $taxQuery, + // "meta_query" => $metaQuery, + 's' => "Atelier Schrauwen", ); $newsPostsDatas = new WP_Query($args); + // write_log($newsPostsDatas->posts); } -// debugQueryTest(); +debugQueryTest();