最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - AJAX Filter WHILE Loop not working Wordpress

programmeradmin0浏览0评论

I have made a AJAX Filter with Wordpress, but my loop is not working. Who can help me with this?

I try to get custom fields back that i use with ACF.

 function filter_reports() {
   global $customer_account;
   $args = array(
     'post_type' => 'ebooks',
     'tax_query' => array(
       'relation' => 'AND',
       array(
         'taxonomy' => 'customer',
         'field'    => 'term_id',
         'terms'    => $customer_account,
       ),
       array(
         'taxonomy' => 'disease',
         'field'    => 'term_id',
         'terms'    => $_POST['options'],
       )
     ),
   );



$the_query = new WP_Query( $args );
$results = array();

   if ( $the_query->have_posts() ) {
     while ( $the_query->have_posts() ) {
    $id =  get_the_ID();
      array_push($results, array(
        'id' => $id,
        'title' => get_field('title', $id),
        'chair' => get_field('e-chair', $id),
      ));
    }
  }

  echo json_encode($results);
  die;

}
  add_action( 'wp_ajax_filter_reports', 'filter_reports' );

I have made a AJAX Filter with Wordpress, but my loop is not working. Who can help me with this?

I try to get custom fields back that i use with ACF.

 function filter_reports() {
   global $customer_account;
   $args = array(
     'post_type' => 'ebooks',
     'tax_query' => array(
       'relation' => 'AND',
       array(
         'taxonomy' => 'customer',
         'field'    => 'term_id',
         'terms'    => $customer_account,
       ),
       array(
         'taxonomy' => 'disease',
         'field'    => 'term_id',
         'terms'    => $_POST['options'],
       )
     ),
   );



$the_query = new WP_Query( $args );
$results = array();

   if ( $the_query->have_posts() ) {
     while ( $the_query->have_posts() ) {
    $id =  get_the_ID();
      array_push($results, array(
        'id' => $id,
        'title' => get_field('title', $id),
        'chair' => get_field('e-chair', $id),
      ));
    }
  }

  echo json_encode($results);
  die;

}
  add_action( 'wp_ajax_filter_reports', 'filter_reports' );
Share Improve this question edited Jan 16, 2017 at 9:52 Dionoh asked Jan 16, 2017 at 9:37 DionohDionoh 1491 silver badge10 bronze badges 4
  • two things, ajax should return things not echo you are not echo array for God's sake , and you are missing wp_ajax_nopriv_... probable. – prosti Commented Jan 16, 2017 at 10:20
  • So what should i do? I am learning. – Dionoh Commented Jan 16, 2017 at 10:21
  • Writing you a reply... – prosti Commented Jan 16, 2017 at 10:22
  • 1 Please consider my answer, as most helpful for now, since no other replies. – prosti Commented Jan 16, 2017 at 10:32
Add a comment  | 

1 Answer 1

Reset to default 0

I think you should organize your code to return things from Ajax call. If this should work for any user you need to use also wp_ajax_nopriv_...

However, I would use dionoh_filter_reports action name.

add_action( 'wp_ajax_dionoh_filter_reports', 'filter_reports' );
add_action( 'wp_ajax_nopriv_dionoh_filter_reports', 'filter_reports' );

I suggest you check this and learn more.

发布评论

评论列表(0)

  1. 暂无评论