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
|
1 Answer
Reset to default 0I 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.
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