handling search

This commit is contained in:
Antoine M 2024-12-02 12:29:28 +01:00
parent 6bcc5a0499
commit de37ae1e7d

View File

@ -31,17 +31,18 @@ function build_search_artisan_posts_cards($request)
$taxonomyIds = explode(',', $StringifiedTaxonomyIds); $taxonomyIds = explode(',', $StringifiedTaxonomyIds);
$taxonomyIds = array_map('intval', $taxonomyIds); $taxonomyIds = array_map('intval', $taxonomyIds);
$search_value = sanitize_text_field($request->get_param('search')) ?? null;
$activePage = is_numeric($previousActivePage) ? $previousActivePage + 1 : 1; $activePage = is_numeric($previousActivePage) ? $previousActivePage + 1 : 1;
$taxQuery = array( $taxQuery = [];
array( if ($taxonomy && !empty($taxonomyIds)) {
$taxQuery[] = array(
'taxonomy' => $taxonomy, 'taxonomy' => $taxonomy,
'terms' => $taxonomyIds, 'terms' => $taxonomyIds,
'field' => 'term_id', 'field' => 'term_id',
) );
); }
$metaQuery = []; $metaQuery = [];
if ($localisation && $localisation !== 'all') { if ($localisation && $localisation !== 'all') {
@ -53,8 +54,6 @@ function build_search_artisan_posts_cards($request)
); );
} }
do_action('wpml_switch_language', $currentLanguage); do_action('wpml_switch_language', $currentLanguage);
$args = array( $args = array(
@ -62,12 +61,13 @@ function build_search_artisan_posts_cards($request)
"post_type" => "artisans", "post_type" => "artisans",
"posts_per_page" => -1, "posts_per_page" => -1,
"paged" => $activePage, "paged" => $activePage,
"tax_query" => $taxQuery, "tax_query" => $taxQuery,
// "meta_query" => [], // "meta_query" => [],
"meta_query" => $metaQuery, "meta_query" => $metaQuery,
's' => $search_value,
); );
$newsPostsDatas = new WP_Query($args); $newsPostsDatas = new WP_Query($args);
// write_log($newsPostsDatas->posts);
ob_start(); ob_start();
@ -95,6 +95,11 @@ function build_search_artisan_posts_cards($request)
$html_template = ob_get_clean(); $html_template = ob_get_clean();
if ($newsPostsDatas->found_posts === 0) {
$html_template = "<div class='no-results'><h3>" . __("Aucun résultat trouvé", "metiers-patrimoine-theme") . "</h3><p>" . __("Essayez d'ajouter un filtre supplémentaire pour trouver plus de résultats", "metiers-patrimoine-theme") . "</p></div>";
}
$response_data = array( $response_data = array(
'html_template' => $html_template, 'html_template' => $html_template,
'total_posts_found' => $newsPostsDatas->found_posts, 'total_posts_found' => $newsPostsDatas->found_posts,
@ -131,9 +136,11 @@ function debugQueryTest()
"status" => "publish", "status" => "publish",
"post_type" => "artisans", "post_type" => "artisans",
"posts_per_page" => -1, "posts_per_page" => -1,
"tax_query" => $taxQuery, // "tax_query" => $taxQuery,
"meta_query" => $metaQuery, // "meta_query" => $metaQuery,
's' => "Atelier Schrauwen",
); );
$newsPostsDatas = new WP_Query($args); $newsPostsDatas = new WP_Query($args);
// write_log($newsPostsDatas->posts);
} }
// debugQueryTest(); debugQueryTest();