0

I have two taxonomy : brand and product. In single sidebar I am displaying the brand and product of the post in the sidebar but I would like to display all the other product of the current taxonomy term brand. This is what I'am going to try but I have no error and it return nothing :

<?php
// get current taxonomy product term
$post_product = get_the_term_list($post->ID, 'product', '', ', ');
echo 'Product : '.$post_marque;

// get current taxonomy brand term
$post_brand = get_the_term_list($post->ID, 'brand', '', ', '); 
echo 'Brand : '.$post_brand;

// make the custom query
  $do_not_duplicate = $post->ID;
            $taxquery = new WP_Query( $taxquery_args ); while($taxquery->have_posts()) : $taxquery->the_post(); 

            if( $post->ID == $do_not_duplicate ) continue;

            $taxquery_args = array(

                'post_type'      => 'any', 
                'posts_per_page' => 10,

                'tax_query' => array(

                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'brand',
                        'field' => 'slug',
                        'terms' => $post_marque
                    )
                    ,
                    array(
                        'taxonomy' => 'product',
                        'field' => 'slug',
                        'terms' => $post_product
                    )
                )
            );

            the_permalink();
            var_dump($taxquery);

            endwhile;  wp_reset_postdata(); ?>

How to return all taxonomy product terms execpt current post term who have current taxonomy brand term of the post ? Is it possible to return term->name and term-slug ?

Screenshot

This is a screenshot, for exemple current post will have taxonomy term brand = Antec and taxonomy product term = P280 and I would like to have all product terms with brand term 'Antec' by term->name except 'P280', so the result shoud return this terms : iSO, GX700, etc...

1 Answer 1

0

You are using get_the_term_list which generates an HTML string of taxonomy terms associated with a post and given taxonomy.

Try instead to feed the terms input parameters of the WP_Query with an array like:

'terms' => array( 11, 22, 33 ),

or

'terms' => array( 'term1', 'term2', 'term3' ),

In your code example you have

'terms' => $post_product

so you could use something like this

$post_product_terms = get_the_terms( $post->ID, 'product' );
$post_product = array();
foreach( $post_product_terms as $term ){
    $post_product[] = $term->slug;    
}

to construct the $post_product array.

If you want to exclude some terms you can use

'operator' => 'NOT IN',

so your example could be:

array(
    'taxonomy' => 'product',
    'field' => 'slug',
    'terms' => $post_product,
    'operator' => 'NOT IN',
)
8
  • Hi, thanks for your answer. The problem is that I don't know ID of the terms and they will always be different. I've tried your solution but it return only the ID of the current post term. I would like to have all the others terms by name.
    – david
    Commented May 28, 2013 at 11:54
  • I updated the answer. Since you are filtering the terms with the slug, I changed $term->term_id to to $term->slug. So want to exclude the current post product terms? Then you can use the operator parameter, see the updated answer.
    – birgire
    Commented May 28, 2013 at 12:17
  • I have the same result, but it return the post term by slug. The post has only one product term, so if I make [code]$post_product_terms = get_the_terms( $post->ID, 'product' ); it constructs an array with only one term but not with all others posts terms ?
    – david
    Commented May 28, 2013 at 13:39
  • this sounds normal, if the post has one product term, the array should only contain one slug element. Did you then add the above 'operator' part into the WP_Query to do the exclusion?
    – birgire
    Commented May 28, 2013 at 14:10
  • yes I did it but it always give me current post term
    – david
    Commented May 28, 2013 at 15:00

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.