I have a custom post type called event. On the archive page I implemented a simple search which returns a selection of posts. Before submitting the search form I'm seeing the proper query vars for the archive page. Once I submit the search form those query vars are removed and return an empty string. I have a few conditionals depending on things like current_post and post_count. Can the ajax call re-populate those variables? How do I get those variables back?

<form id="siteSearchForm" action="<?= admin_url('admin-ajax.php') ?>" method="POST">
    <input type="search" name="s" id="search" value="<?php the_search_query(); ?>" placeholder="Search Events" />
    <input type="hidden" name="action" value="eventsFilter" />
    <input type="submit" id="search-submit" alt="Search" value="Search" class="btn btn-white" />

The JS:

const searchFunctionality = ($) => {
    $('#siteSearchForm').on('submit', function (e) {

        const search = $('#siteSearchForm');

            data: {
                action: 'eventsFilter', // ajax function name
                keyword: $('#search').val(), // search word
            type: search.attr('method'),
            url: search.attr('action'),
            success: function (data) {

        return false;

and finally the AJAX php function:

add_action('wp_ajax_eventsFilter', 'events_filter');
add_action('wp_ajax_nopriv_eventsFilter', 'events_filter');

function events_filter()
    $args = array( // match paramters found in pre_get_posts (/theme.php)
        'meta_key'       => 'event_start_date',
        'order'          => 'ASC',
        'orderby'        => 'meta_value',
        'post_status'    => 'publish',
        'post_type'      => 'event',
        's'              => $_POST["keyword"],
        'tag__not_in'    => array(16),

    $query = new WP_Query($args);

    if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post();
            get_template_part('template-parts/content/content', 'archive');

    else :
        echo 'No posts found';

  • What is the code that reads these query vars and where is it used? Jan 27 at 18:50
  • If you mean the conditionals, I have some if ($wp_query -> current_post === 1 ) else statements on the event archive page. When you land on the page it works, but when you submit the form the current_post variable isn't set any longer. Is that what you meant?
    – Hewe
    Jan 27 at 19:19
  • Your AJAX code replaces .events-wrapper with the AJAX response. It’s not replacing any variables. AJAX can’t do that. If you have logic in your archive template inside the wrapper then that logic would also need to be in the AJAX handler if you’re replacing it. Jan 28 at 4:24
  • The problem is not the ajax response. The problem is that the variables from WP are getting replaced. If before the AJAX executes the query vars contained variables like post_count, after the AJAX response that's gone. I don't think ajax is able to inject things into the PHP variables of WordPress (happy to be wrong here)
    – Hewe
    Jan 30 at 13:31
  • It's literally impossible for variables from WP to be "replaced" by an AJAX response. That's not how any of these technologies work. So I'm trying to understand what's actually happening. What is actually happening that makes you think that the post_count query var is being replaced? Jan 30 at 13:35


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.