skip to Main Content

my url query string is http://website.local/residences/?status=status2&type=apartments&price=null

When I echo $_GET[‘status’] from my page template I get ‘status2’, which is obviously correct.
But I try to get that value from my functions.php it returns false. I’m using ajax to load in more posts with the below function. It should be getting that status value with $_GET for the tax query.. No idea why it’s returning false only in my functions.php.

Any ideas?

function load_more_members($offset = 0, $post_type = 'project'){

    $offset = 0;
    $post_type = 'project';
    $status_array = array();
    $tax_arr = array(
        'relation' => 'AND',
    );

    if(isset($_POST['offset'])) {
        $offset = $_POST['offset'];
    }

    if(isset($_POST['post_type'])) {
        $post_type = $_POST['post_type'];
    }    

    if (isset($_GET['status']) && !empty($_GET['status']) && $_GET['status'] !== 'null') {
        $status = $_GET['status'];
        array_push($status_array, array(
            'taxonomy' => 'status',
            'field' => 'slug',
            'terms' => $status,
        ));
    }


    if (!empty($status_array)) {
        array_push($tax_arr, $status_array);
    }

    $args = array(
        'post_type' => 'project',
        'posts_per_page' => '4',
        'offset' => $offset,
        'order'     => 'DESC',
        'order_by'  => 'date',
          'tax_query' => $tax_arr
    );

    $the_query = new WP_Query( $args );
    
    $posts_left = $offset + 4;
    $found_posts = $the_query->found_posts;

    if ( $the_query->have_posts() ) : 
        ob_start();
        while ( $the_query->have_posts() ) : $the_query->the_post();  
                echo get_template_part('template-parts/latest', 'loop');
        endwhile; 
        $output = ob_get_contents();
        ob_end_clean();
    endif; 
    
    echo json_encode(array(
        'html' => $output,
        'offset' => $posts_left,
        'found_posts' => $found_posts
    ));
    exit();
}

add_action('wp_ajax_nopriv_load_more_members', 'load_more_members');
add_action('wp_ajax_load_more_members', 'load_more_members');
  function load_more_posts(offset, post_type) {
    $.ajax({
      type: "POST",
      url: WPURLS.ajax_url,
      data: {
        action: "load_more_members",
        offset: offset,
        post_type: post_type,
      },
      beforeSend: function (data) {
        $(".load-more-" + post_type).addClass("loading");
      },
      success: function (response) {
        var obj = JSON.parse(response);
        $("." + post_type + "-repeat").append(obj.html);
      },
      error: function (error) {
        console.log("est" + error);
      },
    });
  }
$(".load-more-post").on("click tap touch", function (e) {
    e.preventDefault();
    var this_offset = $(this).attr("data-offset");
    load_more_posts(this_offset, "post");
  });

2

Answers


  1. $_GET will not work as you expect it to work, because you’re trying to get it inside the ajax callback function.

    http://website.local/residences/?status=status2&type=apartments&price=null

    In the above url, you can get the param only when this url loads, but once this url loads and you call a WP Ajax then url becomes like this http://website.local/wp-admin/admin-ajax.php in which there is no param for $_GET

    so you’ll have to collect $_GET when your page loads then store those $_GET values into either js object or in HTML data attributes then you need to pass those values to your js and then you’ll pass those variables as $_POST and will collect them using $_POST in your ajax callback function.

    Login or Signup to reply.
  2. Since you are trying to get it inside of an AJAX callback function, $_GET will not work as you expect.

    As a result, I am providing you with an alternative solution to your problem that I believe will be helpful to you.

    You can get the query string by putting the below function in your JS file.

    function getQueryString() {
        let string = [], hash;
        let hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            string.push(hash[0]);
            string[hash[0]] = hash[1];
        }
        return string;
    }
    

    You can get the value of a query string like let status = getQueryString()["status"]; and pass it to the data in your ajax call.

    function load_more_posts(offset, post_type) {
        let status = getQueryString()["status"];
        $.ajax({
            type: "POST",
            url: WPURLS.ajax_url,
            data: {
                action: "load_more_members",
                offset: offset,
                post_type: post_type,
                status: status
            },
            beforeSend: function (data) {
                $(".load-more-" + post_type).addClass("loading");
            },
            success: function (response) {
                var obj = JSON.parse(response);
                $("." + post_type + "-repeat").append(obj.html);
            },
            error: function (error) {
                console.log("est" + error);
            },
        });
    }
    $(".load-more-post").on("click tap touch", function (e) {
        e.preventDefault();
        var this_offset = $(this).attr("data-offset");
        load_more_posts(this_offset, "post");
    });
    

    Now, you can get the value in your ajax call.

    add_action('wp_ajax_nopriv_load_more_members', 'load_more_members');
    add_action('wp_ajax_load_more_members', 'load_more_members');
    
    function load_more_members()
    {
        echo $_POST['status'];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search