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
    $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',
            'taxonomy' => 'make',
            'terms' => array( $makeTag ),
            'field'    => 'slug',
            'operator'=> 'AND',
            'taxonomy' => 'model',
            'terms' =>  array( $modelTag ),
            'field'    => 'slug',
            'operator'=> 'AND',

    $args = array(
        '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;
                array_push( $term_array, $term_get);

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

  • Try adding $listings->the_post() after while ($listings->have_posts()) {. Feb 26, 2018 at 20:35
  • 1
    @DaveRomsey , thank you! I missed that small part.. its working great, now all have to do is check for taxonomy repeats if I have a 100 cars, Will only list the series once and the count at the side. Feb 27, 2018 at 10:19
  • Glad that helped! I added a little explanation as to why this is necessary below. Feb 27, 2018 at 10:28

1 Answer 1


The loop for $listings should include a call to $listings->the_post() so that the global post object is assigned to the current listing in the loop:

        while ( $listings->have_posts() ) {
            /*** Add the following line: ***/

            $post_get = get_the_ID();
            $term_get = get_the_terms($post_get, 'Car_Series');
            $term_array = $term_get;
            array_push( $term_array, $term_get);

Without this, functions that rely on the global post object, such as get_the_ID() will not return the expected results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.