$_GET not passing into my ajax callback function here is my code.
$_GET not passing therefore I can’t query the posts with terms on WP_Query.
I want to do is ; get $_get values from form via AJAX and Run WP_Query. I use bootstrap multiselect in form.
functions.php ;
add_action('wp_ajax_my_ajax_filter_search', 'my_ajax_filter_search_callback');
add_action('wp_ajax_nopriv_my_ajax_filter_search', 'my_ajax_filter_search_callback');
function my_ajax_filter_search_callback() {
header("Content-Type: application/json");
$meta_query = array('relation' => 'AND');
$tax_query = array();
if($_GET['app_category_multi']) {
$app_cat = sanitize_text_field( $_GET['app_category_multi'] );
$tax_query[] = array(
'taxonomy' => 'app_category',
'field' => 'slug',
'terms' => $app_cat,
'operator' => 'AND'
);
}
$args = array(
'post_type' => 'playbook',
'posts_per_page' => -1,
'meta_query' => $meta_query,
'tax_query' => $tax_query
);
$search_query = new WP_Query( $args );
if ( $search_query->have_posts() ) {
$result = array();
while ( $search_query->have_posts() ) {
$search_query->the_post();
$result[] = array(
"title" => get_the_title(),
"permalink" => get_permalink(),
"short_images" => get_short_images(),
"get_tags" => get_tags_item(),
"short_description" => get_field('short_description')
);
}
wp_reset_query();
echo json_encode($result);
} else {
// no posts found
}
wp_die();
}
Thanks a lot.
2
Answers
Problem is from Bootstrap Multiselect library. I added 3 hidden inputs for them. After that I updated their value with jQuery.
Input value's have been turned to array with php's explode and put in tax_query with WP_Query.
If you have this "rare" problem like this, It's not from WordPress. It's from multiselect lib.
Thanks a lot to all contributers.
Using the wordpress function
get_query_var()
you can do the following:and then in your code
more information is available in the wordpress documentation – https://developer.wordpress.org/reference/functions/get_query_var/