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

发布于 2025-01-16 08:25:44 字数 4308 浏览 0 评论 0原文

当我点击分页链接(页码)时,它会给我 post_type、taxonomy、term_name。我将带有 JQuery 变量的变量传递给 Ajax wordpress 函数。 Ajax 函数正在接收变量。但 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 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。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女的英雄梦 2025-01-23 08:25:44

我自己解决了。

问题出在 wp_query 循环中。我使用自定义 $args 和循环对象是 $the_query。

所以,

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文