I have 3 custom taxonomies. The first 2 are for car make and model, the 3rd is for car series. The goal is make a widget which lists car series' depending on current post (make and model).
function getCarListings() {
global $post;
//$post_id = $post->id;
setup_postdata( $post );
$post_id = get_the_ID();
echo '<h2>' . $post_id . '</h2>';
$makeArray = get_the_terms($post_id, 'make'); //make taxonomy for current post
//print_r($makeArray);
$makeTag = $makeArray[0]->name;
//print_r($makeTag );
$modelArray = get_the_terms($post_id, 'model'); //model taxonomy for current post
$modelTag = $modelArray[0]->name;
//add_image_size( 'realty_widget_size', 85, 45, false );
$mytaxomomies= array(
// 'relation' => 'AND',
array(
'taxonomy' => 'make',
'terms' => array( $makeTag ),
'field' => 'slug',
'operator'=> 'AND',
),
array(
'taxonomy' => 'model',
'terms' => array( $modelTag ),
'field' => 'slug',
'operator'=> 'AND',
)
);
$args = array(
'post_type'=>'listings',
'tax_query'=> $mytaxomomies,
);
$listings = new WP_Query($args);
$term_array = [];
// $listings->query('post_type=listings&posts_per_page=-1' );
if($listings->found_posts > 0) {
while ($listings->have_posts()) {
$post_get = get_the_ID();
$term_get = get_the_terms($post_get, 'Car_Series');
$term_array = $term_get;
print_r($term_get);
array_push( $term_array, $term_get);
}
print_r($term_array);
wp_reset_postdata();
}else{
echo '<p style="padding:25px;">No listing found</p>';
}
}
The top part gets the post id of the main page, which I am able to retrieve the car make and car Model. I then make a loop with these 2 taxonomies, trying to retrieve the 3rd taxonomy terms into an term_array. But it seems print_r($term_array)
is blank but print_r($term_get)
shows my WP object.
Why can I not retrieve collection of terms on that last array ($term_array
)?
$listings->the_post()
afterwhile ($listings->have_posts()) {
.