I'm trying to display 4 related posted in the same category or have the same tag with the current post on the single post page. The result is just blank.

      $rCateogry = wp_get_post_categories($post->ID);
      $rTag = wp_get_post_tags($post->ID);
      $args = 
          'tax_query' => array(
            'relation' => 'OR',
              'taxonomy' => 'category',
              'field'    => 'id',
              'terms'    => $rCateogry,
              'taxonomy' => 'tag',
              'field'    => 'id',
              'terms'    => $rTag,
          'numberposts' => 4,
          'post__not_in' => array($post->ID)
      $query = new WP_Query( $args );
    <?php if ( $query->have_posts() ) : ?>
      <?php while ( $query->have_posts() ) { ?>
        <?php $query->the_post(); ?>
        <li class="">
          <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
          <div class="feat-img">
            <?php if ( has_post_thumbnail()) : ?>
              <?php the_post_thumbnail("custom-ss"); ?>
            <?php endif ?>
          <p><?php the_title(); ?></p>
          <?php get_template_part('templates/entry-meta'); ?> 
      <?php } wp_reset_postdata(); ?>
    <?php endif; ?>
  • The built in tag taxonomy is post_tag, not just tag. also, it's odd that the codex example for tax query shows field as id, but I'm fairly sure it should be term_id. try those two changes and see what your results are. also note that you can var_dump( $query ) after you run the query and see if the query parameters are correctly set, anything invalid should be ignored. – Milo Aug 15 '14 at 0:33
  • Thanks, I've already fixed it earlier. You are right, changing to post_tag does the trick. I had field as id, and it did work, but I have follow your suggestion and change it to term_id, which also works. – user58208 Aug 15 '14 at 6:25

Your Answer

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

Browse other questions tagged or ask your own question.