WP_QUERY: Tax_query returns posts only from the first element in ‘terms’?

I have been trying to use FETCH API to return a string of values of all the checkboxes on my template. It seem to be passing the correct “args” when I checked the Dev Console. But still all the post are only from the first element of the arrays all_ages, all_paths, paths and ages.

$all_ages = wp_list_pluck(get_terms(array(‘taxonomy’ => ‘age’, ‘hide_empty’ => false)), ‘slug’);
$all_paths = wp_list_pluck(get_terms(array(‘taxonomy’ => ‘path’, ‘hide_empty’ => false)), ‘slug’);
if (!empty($_POST[‘pathQuery’])) {
$paths = $_POST[‘pathQuery’];
$paths = explode(‘,’, $paths);
$path_query = array(‘taxonomy’ => ‘path’, ‘field’ => ‘slug’, ‘terms’ => (object)$paths, ‘operator’ => ‘IN’, ‘include_children’ => true,);
}
else {
$path_query = array(‘taxonomy’ => ‘path’, ‘field’ => ‘slug’, ‘terms’ => (object)$all_paths , ‘operator’ => ‘IN’, ‘include_children’ => true,);
}
if (!empty($_POST[‘ageQuery’])) {
$ages = $_POST[‘ageQuery’];
$ages = explode(‘,’, $ages);
$age_query = array(‘taxonomy’ => ‘age’, ‘field’ => ‘slug’, ‘terms’ =>(object)$ages, ‘operator’ => ‘IN’, ‘include_children’ => true,);
}
else {
$age_query = array(‘taxonomy’ => ‘age’, ‘field’ => ‘slug’, ‘terms’ => (object)$all_ages, ‘operator’ => ‘IN’, ‘include_children’ => true,);
}
$args = array(
‘post_type’ => ‘regular_course’,
‘posts_per_page’ => -1 ,
‘tax_query’ => array(
‘relation’ => ‘AND’,
$path_query,
$age_query,
),
);
echo “<script>console.log(‘” . json_encode($args) . “‘)</script>”;
$query = new WP_Query($args);
$the_posts = $query->get_posts();
foreach ($the_posts as $post) {
echo “<div class=’col-6 course’>”;
echo “<h3>”. $post->post_title . ” </h3>”;
echo “<button class=’col-10′> Book </button>”;
echo “</div>”;
}
wp_die();

Read more here:: WP_QUERY: Tax_query returns posts only from the first element in ‘terms’?

Leave a Reply

Your email address will not be published. Required fields are marked *