I want to to show my customer works in a slide This is my query to show these works. I want to obtain the term of the taxonomy "idm_real_tax" for each custom type record.

$query = new WP_Query($criterias);
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $posts[] = array(
            "id" => $query->post->ID,
            "permalink" => get_permalink($query->post->ID),
            "title" => $query->post->post_title,
            "excerpt" => $query->post->post_excerpt,
            "thumbnail" => get_the_post_thumbnail($query->post->ID , "carrousel-realisation"),
            "pager_thumbnail" => get_the_post_thumbnail($query->post->ID , "thumbnail", array( 'class' => 'img-thumbnail img-responsive' )), 
            // "term_single" => get_the_terms( $query->$post->id, "idm_real_tax" ),

The problem is this line, that I commented :

"term_single" => get_the_terms( $query->$post->id, "idm_real_tax" ),
  • How many terms do you want - just one or all of them? And how do you want "the term" - As an id, name, slug, description, or an object/array of everything?
    – David Gard
    Feb 18 '15 at 14:56

I'm not sure exactly how you want 'term_single' formatted, but you can amend the below as required if it's not quite what you are looking for.

I'd also suggest not using the name $posts for your array, as I believe that WP has a global of the same name, so it could cause funkyness that is undesirable.

Finally, as you were in The Loop you can use simple functions to get all of the info that you require (ID, name, etc.) - check out the Codex.

$query = new WP_Query($criterias);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();

        /** Get all terms (as objects) from the 'idm_real_tax' taxonomy that are associated with this Post */
        $terms = get_the_terms(get_the_ID(), 'idm_real_tax');

        /** Check that '$terms' is not empty */
        if($terms && !is_wp_error($terms)) :    // It's not...

            $term_names = array();  // Pre-stage to avoid errors

            /** Loop through each term object and add just the name to the '$term_names' array */
            foreach($terms as $term) :
                $term_names[] = $term->name;

            /** Join all of the term names together to make a comman seperated string of term names */
            $term_names_string = (!empty($term_names)) ? join(", ", $term_names) : '';


        $my_posts[] = array(
            'id'                => get_the_ID(),
            'permalink'         => get_permalink(),
            'title'             => get_the_title(),
            'excerpt'           => get_the_excerpt(),
            'thumbnail'         => get_the_post_thumbnail(get_the_ID(), 'carrousel-realisation'),
            'pager_thumbnail'   => get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class' => 'img-thumbnail img-responsive')), 
            'term_single'       => $term_names


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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