繁体   English   中英

我正在尝试通过 ajax 调用检索帖子,但 Wordpress Ajax 给出服务器错误 500

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

当我点击分页链接(页码)时,它会给我 post_type、taxonomy、term_name。 我将带有 JQuery 变量的变量传递给 Ajax wordpress function。Ajax function 正在接收变量。 但是 WP_Query 循环不适用于传递的数据。

JavaScript 代码:

<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');

在此处输入图像描述

我正在尝试通过 Ajax 请求进行分页。 其他所有脚本都在工作。 但这是一个单一的分类页面。 在此页面上,循环不执行。 它给出服务器错误 500。

我自己解决了。

问题出在 wp_query 循环中。 我使用自定义 $args,循环 object 是 $the_query。

所以,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM