1

I have someting like this:

$terms = get_the_terms( get_the_ID(), 'kosmetyki_dystrybutor'); 
$terms_ids = [];

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

$args = array(
    'post_type' => 'kosmetyki',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'kosmetyki_dystrybutor',
            'field'    => 'term_id',
            'terms'    => array($term->term_id) 
        )
    ),
);

$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
   //here posts

<?php endwhile;
endif;?>

But not working. I need posts from taxonomy 'kosmetyki_dystrybutor'. Each 'dystrybutor' need own posts list. What I do wrong? Thx for help :)

  • 1) Where is $term coming from? 2) Have you tried anything where //here posts is? the_post() doesn't do anything on its own. – Jacob Peattie Sep 27 '17 at 10:53
  • Where is $term coming from? – FluffyKitten Sep 27 '17 at 10:53
  • I add full code. In //here posts I need posts with <?php the_title(); ?> and <?php the_content(); ?> – Silva Sep 27 '17 at 11:00
0

$term isn't being set anywhere in your code. You're putting IDs into $terms_ids, but then in your query you're using $term->term_id. $term doesn't exist outside of the foreach. You need to be passing $terms_ids directly into terms:

$terms = get_the_terms( get_the_ID(), 'kosmetyki_dystrybutor'); 
$terms_ids = [];

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

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

$query = new WP_Query($args);
  • Thx, but the problem still exist :( – Silva Sep 27 '17 at 11:14
  • Maybe all my code is wrong? I need list like here czytamyetykiety.pl/manufacturer/babybel/ but with taxonomy not category. – Silva Sep 27 '17 at 11:15
  • Where is this code? get_the_terms( get_the_ID(), 'kosmetyki_dystrybutor'); will get the kosmetyki_dystrybutor terms of the current post. Is it within the loop, and in a post that would have terms in the kosmetyki_dystrybutor taxonomy? – Jacob Peattie Sep 27 '17 at 11:39
  • Hmm, the code is on "dystrybutor" page like this one czytamyetykiety.pl/kosmetyki_dystrybutor/costasy – Silva Sep 27 '17 at 11:52
  • I choose "dystrybutor" here czytamyetykiety.pl/dystrybutorzy and next step is dystrybutor page where I need list of posts for selected "dystrybutor" – Silva Sep 27 '17 at 12:11

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.