1

Thanks for any help.

I am in a custom post (not an archive) with a taxonomy. And I'd like to display :

  • some other custom posts
  • with this current taxonomy

Doesn't seems so hard but for me it does... I didn't find the right way to use the term of my tax in the query...

Here is (one of) my try :

$terms = wp_get_post_terms( $post->ID, 'identite'); // to get my taxonomy

foreach ( $terms as $term ) {
echo "$term->slug"; // just for test - ok

   $args = array(
    'post_type' => 'example',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'identite',
            'field'    => 'ID',
            'terms'    => $terms
             )
        ),
     );// end args

$query = new WP_Query( $args);

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
         $query->the_post();

       // Little pray, but doesn't work

    }//end of while

}

I obtain this error message : Object of class WP_Term could not be converted to int in

Any idea to convert my object and make it readable ? Thanks a lot

(edit : I try with the function wp_list_pluck but without success)

1
  • Did you want to display the posts for each term? Or just a list of non separated posts?
    – ngearing
    Jan 10, 2017 at 5:17

2 Answers 2

1

Try this for WP_Query

$args = array(
'post_type' => 'example',
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'identite',
        'field'    => 'ID',
        'terms'    => $term->term_id
         )
    ),
 );// end args

OR

$args = array(
'post_type' => 'example',
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'identite',
        'field'    => 'ID',
        'terms'    => array($term->term_id)
         )
    ),
 );// end args

$term is an object and tax_query expects an array of id's.

See: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

0
0

You get this error message because of the way you are setting up the $args array. In the tax_query, you'll want to pass an array of all the terms ids for the current post. Also, the field value was incorrect (as seen in the Codex here: WP_Query#Taxonomy_Parameters).

The final code should look something like:

<?php 

$terms = wp_get_post_terms( $post->ID, 'identite'); 
$terms_ids = [];

foreach ( $terms as $term ) {
    $terms_ids[] = $term->term_id;
}

$args = array(
    'post_type' => 'example',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'identite',
            'field'    => 'term_id',
            'terms'    => $terms_ids
        )
    ),
);

$query = new WP_Query($args);

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();

        // All the magic here
    }
}
2
  • Thanks Marc-Antoine ! I just replaced in the query : 'terms' => $terms_ids by 'terms' => array($term->term_id) And the conversion works. Un grand merci !
    – Cha
    Jan 10, 2017 at 10:04
  • My pleasure! Although be aware that this snippet will now only load posts contained in the last category (if you kept the foreach part on top). As wp_get_post_terms might return an array of categories, the value of $term after the foreach will be left at the its last value in the foreach. The main point of my sample code was to have posts loaded for every categories that would be selected for the current post. Jan 10, 2017 at 15:35

Your Answer

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

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