I’ve a customized submit kind referred to as occasion
. On the archive web page I applied a easy search which returns a collection of posts. Earlier than submitting the search type I am seeing the correct question vars for the archive web page. As soon as I submit the search type these question vars are eliminated and return an empty string. I’ve a number of conditionals relying on issues like current_post and post_count. Can the ajax name re-populate these variables? How do I get these variables again?
<type id="siteSearchForm" motion="<?= admin_url('admin-ajax.php') ?>" technique="POST">
<enter kind="search" title="s" id="search" worth="<?php the_search_query(); ?>" placeholder="Search Occasions" />
<enter kind="hidden" title="motion" worth="eventsFilter" />
<enter kind="submit" id="search-submit" alt="Search" worth="Search" class="btn btn-white" />
</type>
The JS:
const searchFunctionality = ($) => {
$('#siteSearchForm').on('submit', operate (e) {
e.preventDefault();
const search = $('#siteSearchForm');
$.ajax({
information: {
motion: 'eventsFilter', // ajax operate title
key phrase: $('#search').val(), // search phrase
},
kind: search.attr('technique'),
url: search.attr('motion'),
success: operate (information) {
$('.events-wrapper').html(information);
},
});
return false;
});
};
and at last the AJAX php operate:
<?php
add_action('wp_ajax_eventsFilter', 'events_filter');
add_action('wp_ajax_nopriv_eventsFilter', 'events_filter');
operate events_filter()
{
$args = array( // match paramters present in pre_get_posts (/theme.php)
'meta_key' => 'event_start_date',
'order' => 'ASC',
'orderby' => 'meta_value',
'post_status' => 'publish',
'post_type' => 'occasion',
's' => $_POST["keyword"],
'tag__not_in' => array(16),
);
$question = new WP_Query($args);
if ($query->have_posts()) :
whereas ($query->have_posts()) : $query->the_post();
get_template_part('template-parts/content material/content material', 'archive');
endwhile;
wp_reset_postdata();
else :
echo 'No posts discovered';
endif;
die();
}