// Encolar el JS del autocompletado y pasarle la URL de AJAX add_action('wp_enqueue_scripts', function () { $path = get_stylesheet_directory() . '/js/pmx-live-search.js'; $ver = file_exists($path) ? filemtime($path) : '1.0'; wp_enqueue_script( 'pmx-live-search', get_stylesheet_directory_uri() . '/js/pmx-live-search.js', [], $ver, true ); wp_localize_script('pmx-live-search', 'PMX_LS', [ 'ajax' => admin_url('admin-ajax.php'), ]); }); add_action('wp_ajax_pmx_live_search', 'pmx_live_search'); add_action('wp_ajax_nopriv_pmx_live_search', 'pmx_live_search'); function pmx_live_search() { if (empty($_GET['q'])) wp_send_json_success([]); $term = sanitize_text_field(wp_unslash($_GET['q'])); $pt = isset($_GET['pt']) ? array_map('sanitize_key', (array) $_GET['pt']) : ['listing','oferta']; $args = [ 'post_type' => $pt, 'post_status' => 'publish', 's' => $term, 'posts_per_page' => 8, 'orderby' => 'date', 'order' => 'DESC', ]; if (!empty($_GET['city'])) { $city = sanitize_text_field($_GET['city']); $args['meta_query'] = [ 'relation' => 'OR', ['key'=>'city','value'=>$city,'compare'=>'LIKE'], ['key'=>'listeo_city','value'=>$city,'compare'=>'LIKE'], ['key'=>'location_city','value'=>$city,'compare'=>'LIKE'], ]; } $q = new WP_Query($args); $CITY_KEYS = ['city','listeo_city','location_city','_geolocation_city']; $PRICE_KEYS = ['base_price','pmx_base_price','_regular_price','_price']; $items = []; while ($q->have_posts()) { $q->the_post(); $id = get_the_ID(); $img = get_the_post_thumbnail_url($id, 'thumbnail'); if (!$img) $img = get_stylesheet_directory_uri().'/assets/img/placeholder.png'; $city = ''; foreach ($CITY_KEYS as $k) { $v = get_post_meta($id, $k, true); if ($v) { $city = $v; break; } } $price = ''; foreach ($PRICE_KEYS as $k) { $v = get_post_meta($id, $k, true); if ($v !== '' && $v !== null) { $price = $v; break; } } $items[] = [ 'id' => $id, 't' => html_entity_decode(get_the_title(), ENT_QUOTES), 'u' => get_permalink($id), 'img' => $img, 'city' => $city, 'price' => $price, 'pt' => get_post_type($id), ]; } wp_reset_postdata(); wp_send_json_success($items); } Gente disponibles ~ PlanMix
Home
Account
Cart
Search