skip to Main Content

$_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


  1. Chosen as BEST ANSWER

    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.


  2. Using the wordpress function get_query_var() you can do the following:

    function my_ajax_filter_search_query_vars( $qvars ) {
        $qvars[] = 'app_category_multi';
        return $qvars;
    }
    add_filter( 'query_vars', 'my_ajax_filter_search_query_vars' );
    

    and then in your code

    
    if(get_query_var( 'app_category_multi')) {
        $app_cat = sanitize_text_field( get_query_var( 'app_category_multi') );
        $tax_query[] = array(
            'taxonomy' => 'app_category',
            'field' => 'slug',
            'terms' => $app_cat,
            'operator' => 'AND'
        );
    }
    

    more information is available in the wordpress documentation – https://developer.wordpress.org/reference/functions/get_query_var/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search