I have a filter form that automatically submits as the user selects filter parameters, and displays loads via AJAX. That’s working fine but I can’t seem to get the search data to return the correct values when the search results update.
I would like to output something like this:
Travel Month: January
Destinations: Grand Canyon, Las Vegas
Activities: Great for Kids, Photography
Here is my JS script:
(function($){
$(document).on("sf:ajaxfinish", ".searchandfilter", function(){
console.log("ajax complete");
$.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: { action: 'filterParams'}
}).done(function( response ) {
$( ".parameters" ).html( response );
});
});
})(jQuery);
And here is the PHP script that I put in functions.php:
function filterParams(){
global $searchandfilter;
$sf_current_query = $searchandfilter->get(12192)->current_query();
$args = array(
"str" => '%1$s: %2$s', // this is the format of the string generated for each field
"delim" => array(", ", " - "),
"field_delim" => '<br />',
"show_all_if_empty" => true
);
echo $sf_current_query->get_fields_html(
array("_sft_travelmonth","_sft_destination", "_sft_activities"),
$args
);
}
add_action('wp_ajax_filterParams', 'filterParams');
add_action('wp_ajax_nopriv_filterParams', 'filterParams');
This is what is always getting returned in the AJAX call:
Travel Months: All Travel Months
Destinations: All Destinations
Activities: All Activities0
When I use:
var_dump($sf_current_query->get_array());
instead of:
echo $sf_current_query->get_fields_html(
array("_sft_travelmonth","_sft_destination", "_sft_activities"),
$args
);
It returns an empty array:
array(0) { } 0
I’ve confirmed that the form ID is correct.
What am I missing here?