0

I've been searching on this topic for 1 hour but I just can't seem to figure it out!

Im using this WP_Query to loop trough related posts and create clickable links:

<?php
        $currentID = get_the_ID();

        $args = array( 
          'post_type'       => 'nieuws',
          'orderby'         => 'date',
          'post__not_in'    => array($currentID),
          'nieuwssoort'     => current$term->slug,
          'posts_per_page'  => '7',
          );

        $the_query = new WP_Query( $args );
      ?>

      <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

everything works great but I need to show post with the corresponding taxonomy term instead of all nieuws posts. my taxonomy is called nieuwssoort and I need to dynamically fill in the term, I know I need to start with this probably: $terms = get_the_terms( $post->ID, 'nieuwssoort' ), but just can't figure out the rest and how to include it in the new WP_Query( $args ) arguments array so it will fill out the current posts's nieuwssoort term in 'nieuwssoort' => ''

Thanks!

0

This gets the terms:

$terms = get_the_terms( get_the_ID(), 'nieuwssoort');

Then you check the result:

 $terms_ids = [];//you can set a default id here
    if(false !== $terms && !is_wp_error($terms)){
        foreach($terms as $term){
             $terms_ids[] = $term->term_id;
         }
    }

We check the result just in case.

Now, we have the values, let get them into the arguments:

$args = array( 
          'post_type'       => 'nieuws',
          'orderby'         => 'date',
          'post__not_in'    => array($currentID),
          'tax_query' => array(
                           array(
                            'taxonomy' => 'nieuwssoort',            
                            'terms'    => $terms_ids
                           ),
          ),

          'posts_per_page'  => '7',
      );

I can't test the code right now, but I think it should work.

2
  • Perfect! Thanks! What exactly are you doing in the second part where you check the result? – Boris Kamp Sep 20 '14 at 11:41
  • @BorisKamp, I'm checking if the get_the_terms worked and the result is right. You can read here – Tomás Cot Sep 20 '14 at 15:18

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.