2

I have been trying to output content of all post-types (Posts, Pages and CPTs) based on a term of a custom taxonomy that they must share (meaning if they don't share that particular term, the output should not include that post-type).

Here is what I have so far:

$term_list = wp_get_post_terms($post->ID, 'persons', array('fields' => 'names')); // persons is the custom taxonomy
    $args = array(
        'post_type' => array( 'post', 'page', 'profile', 'letter' ), // profile and letter are CPTs
            'tax_query' => array(
                array(
                'taxonomy' => 'persons',
                'field' => 'slug',
                'terms' => $term_list
                )
            ),
        'post__not_in'=>array($post->ID)
    );

$related_content = new WP_Query( $args );

if ( $related_content->have_posts() ) {
    echo __('Related Content (profiles, pages, articles, letters):', 'teselle');
    echo '<ul class="related-content">';
    while ($related_content->have_posts()) { 
        $related_content->the_post();

        echo '<li><a href="' . esc_url( get_permalink() ) . '">' . get_the_title() . '</a></li>';

    } // endwhile
    echo '</ul>';
} // endif

wp_reset_query(); wp_reset_postdata();

The problem with the above code is that it outputs too much.

If I fill in the exact slug of the term I need, for example 'terms' => 'the-exact-slug', it works perfect, but I really need it to be a variable.

Can someone point out what my mistake is in the above code?

Thanks!

1

we meet here again :)

Try using this:

$term_list = wp_get_post_terms( $post->ID, 'persons', array( 'fields' => 'ids' ) );

and

'tax_query' => array(
     array(
         'taxonomy' => 'persons',
         'field' => 'id',
         'terms' => $term_list
         )
     ),

AFAIK, the tax_query accepts field by id or slug only (see here. And the wp_get_post_terms accepts only names (not slug), ids and all. So the match between them is only id.

Update

If you need slug, use this:

$terms = wp_get_post_terms( $post->ID, 'persons' );
$term_slugs = wp_list_pluck( $terms, 'slug' );
3
  • thanks for your input @Rilwis. This outputs the correct IDs, but how to get the slug (that I really need) from those IDs?
    – user2015
    Feb 7 '13 at 3:21
  • Updated the answer with code to get term slugs.
    – Anh Tran
    Feb 7 '13 at 4:03
  • that works indeed, in the tax_query I changed the 'field' back to 'slug' thanks so much! On to the next challenge as this only outputs the related profile CPT, not other content :)
    – user2015
    Feb 7 '13 at 4:45
0

You might try the slugs instead of the names, i.e.

$term_list = wp_get_post_terms($post->ID, 'persons', array('fields' => 'slug')); 

instead of

$term_list = wp_get_post_terms($post->ID, 'persons', array('fields' => 'names')); 

since your are using

'field' => 'slug',

in the tax query.

EDIT:

$term_list = wp_get_post_terms($post->ID, 'persons', array('fields' => 'ids')); // persons is the custom taxonomy
    $args = array(
        'posts_per_page'=>-1,
        'post_type' => array( 'post', 'page', 'profile', 'letter' ), // profile and letter are CPTs
            'tax_query' => array(
                array(
                'taxonomy' => 'persons',
                'field' => 'id',
                'terms' => $term_list
                )
            ),
        'post__not_in'=>array($post->ID)
    );

I added posts_per_page to return all results and use ids in the wp_get_post_terms and id in the tax_query.

8
  • Thanks for the suggestion, but that is not it. If I change it to slug the thing falls apart, if I change it to slugsnothing changes.
    – user2015
    Feb 7 '13 at 0:03
  • ok, I updated the code above, let's see what happens ;-)
    – birgire
    Feb 7 '13 at 1:34
  • still outputs content that is not linked in any way...
    – user2015
    Feb 7 '13 at 2:04
  • strange, it seems to work on my install (tested only with one cpt)
    – birgire
    Feb 7 '13 at 2:09
  • maybe you can debug it with print_r($term_list); and then try for example some handpicked array on 'terms' => array(123,321) using the relevant ids.
    – birgire
    Feb 7 '13 at 2:15

Your Answer

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