Retrieve Custom Taxonomies according to posts selected

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) ?

Read more here:: Retrieve Custom Taxonomies according to posts selected

Leave a Reply

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