php - AJAX Filter WHILE Loop not working Wordpress

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 f

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745144565a4613588.html

相关推荐

  • php - AJAX Filter WHILE Loop not working Wordpress

    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 f

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信