Shahid Latif
Shahid Latif

Reputation: 19

I am trying to retrieve the posts via ajax call but Wordpress Ajax gives server error 500

When I click on pagination link (page number) it gives me post_type, taxonomy, term_name. I passed the variables with JQuery Variables to Ajax wordpress function. The Ajax function is receiving the vaiables. But WP_Query loop does not work with the passed data.

JavaScript Code:

<script>
 $(".category--pagination").on("click", "a", function (e) {
    e.preventDefault();
    var link = $(this).attr("href");
    if (link.indexOf("/page/") >= 0) {
      brands_page = parseInt(link.substr(link.indexOf("/page/") + 6, 4));
    } else {
      brands_page = 1;
    }
    mzm_filter_taxonomy();
  });

  //  mzm_filter_tax

  function mzm_filter_taxonomy() {
    $("#brandCategoryItemDisplay").addClass("loading");
    var post_type = $("#post_type").val();
    var taxonomy = $("#taxonomy").val();
    var term_slug = $("#term_slug").val();
    var term_name = $("#term_name").val();
    //console.log(post_type);
   // console.log(taxonomy);
   // console.log(term_name);
   if (brands_page == undefined) {
    brands_page = 1;
  }
    var data = {
      action: "mzm_filter_taxonomy",
      post_type: post_type,
      taxonomy:taxonomy,
      term_slug:term_slug,
      term_name:term_name,
      page:brands_page,

    };

    $.post(mzm_filter.ajax_url, data, function (response, textStatus, jqXHR) {
      if (response.success) {
        console.log("executed success");
        $("#brandCategoryItemDisplay").removeClass("loading");
        $("#brandCategoryItemDisplay").html(response.data.items);

        $(".category--pagination").html(response.data.pagination);
        //$("#taxonomy_pagination").html(response.data.pagination);
      }
    });
  }
</script>

Wordpress Ajax Function:

function mzm_filter_taxonomy()
{
    $post_type   = isset($_POST['post_type']) ? filter_var(trim($_POST['post_type']), FILTER_SANITIZE_FULL_SPECIAL_CHARS) : null;
    $taxonomy   = isset($_POST['taxonomy']) ? filter_var(trim($_POST['taxonomy']), FILTER_SANITIZE_FULL_SPECIAL_CHARS) : null;
    $term_slug   = isset($_POST['term_slug']) ? filter_var(trim($_POST['term_slug']), FILTER_SANITIZE_FULL_SPECIAL_CHARS) : null;
    $term_name   = isset($_POST['term_name']) ? filter_var(trim($_POST['term_name']), FILTER_SANITIZE_FULL_SPECIAL_CHARS) : null;
    $perpage  = mzm_brand_perpage();
    $paged    = (isset($_POST['page']) && intval($_POST['page'])) ? intval($_POST['page']) : 1;
    // $sort     = ( isset( $_POST['sort'] ) && intval( $_POST['sort'] ) ) ? intval( $_POST['sort'] ) : 1;
   // $sort     = (isset($_POST['sort'])) ? intval($_POST['sort']) : 1;
    // $args     = array(
    //     'post_type'      => $post_type,
    //     'hide_empty'     => false,
    //     'posts_per_page' => $perpage,
    //     'paged'          => $paged,
    //     'post_status' => 'publish',
    //     'tax_query' => array(
    //         array (
    //             'taxonomy' => $taxonomy,
    //             'field' => 'slug',
    //             'terms' => $term_slug,
    //         )
    //     ),
    // );
    $the_query = new WP_Query( array(
        'post_type' => $post_type,
        'tax_query' => array(
            array (
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $term_slug,
            )
        ),
    ) );
   // $the_query = new WP_Query($args);
    ob_start();
    // echo $post_type . '<br>';
    // echo $taxonomy . '<br>';
    // echo $term_name . '<br>';
    // echo $term_slug . '<br>';
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            the_post();
           // echo mzm_render_single_brand_card();
           echo the_title();
        }
        wp_reset_postdata();
    } else {
        echo '<div class="no-criteria-search">Sorry, no posts matched your criteria.</div>';
    }
    $html = ob_get_clean();
    $result = array(
        'items' => $html,
        'pagination' => 'mzm_render_pagination($the_query)',
    );
    wp_send_json_success($result);
}
add_action('wp_ajax_nopriv_mzm_filter_taxonomy', 'mzm_filter_taxonomy');
add_action('wp_ajax_mzm_filter_taxonomy', 'mzm_filter_taxonomy');

enter image description here

I am trying paginate via Ajax request. Other all scripts are working. But this is a single taxonomy page. On this page the loop doesn't executes. It gives server error 500.

Upvotes: 0

Views: 285

Answers (1)

Shahid Latif
Shahid Latif

Reputation: 19

I solved it myself.

The problem was in wp_query loop. I using custom $args and the loop object was $the_query.

So,

the_post();
//should be
$the_query->the_post();

Upvotes: 1

Related Questions