'GET', 'callback' => 'build_search_artisan_posts_cards', 'permission_callback' => '__return_true', )); }); function build_search_artisan_posts_cards($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; do_action('wpml_switch_language', $currentLanguage); // $lang = apply_filters('wpml_current_language', null); $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 = []; 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( 'key' => 'state', 'value' => $localisationArray, 'compare' => 'IN', // Comparer avec plusieurs valeurs ); } $args = array( "post_status" => "publish", "post_type" => "artisans", "posts_per_page" => -1, "paged" => $activePage, "tax_query" => $taxQuery, // "meta_query" => [], "meta_query" => $metaQuery, 's' => $search_value, 'orderby' => 'title', 'order' => 'ASC', ); $searchArtisans = new WP_Query($args); ob_start(); foreach ($searchArtisans->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; get_template_part( 'template-components/artisans/card-artisans-search', null, array( 'card_variant' => 'activite', 'post_ID' => $post->ID, 'post_title' => get_the_title($post->ID), 'post_thumbnail' => $post_thumbnail, 'news_type' => $news_type, 'post_date' => $post_date, 'current_taxonomy' => $taxonomy, ) ); } $html_template = ob_get_clean(); if ($searchArtisans->found_posts === 0) { ob_start(); get_template_part( 'template-components/artisans/artisan-search-no-results', null, [] ); $html_template = ob_get_clean(); } $response_data = array( 'html_template' => $html_template, 'total_posts_found' => $searchArtisans->found_posts, 'active_page' => $activePage, 'max_num_pages' => $searchArtisans->max_num_pages, ); $response = new WP_REST_Response($response_data); $response->set_status(200); return $response; }