0

I want to show a list of posts with the same terms as the post you are currently viewing (it's basically a related posts list).

It's a custom post type (called "oferta") and a custom taxonomy ("categoría").

I want to show 5 posts maximum (not all) as an ul list.

I'm using most of the code from https://wordpress.org/support/topic/query-cpt-that-share-the-same-taxonomy-as-the-current-cpt/

                $custom_terms = wp_get_post_terms( get_the_ID(), 'categoria');
                $args = array(
                  'post_type' => 'oferta',
                  'tax_query' => array(             
                       array(
                          'taxonomy' => 'categoria',
                          'posts_per_page' => 5,
                          'field' => 'slug',
                          'terms' => $custom_terms[0]->slug, 
                      ),
                   )
                );

                $loop = new WP_Query( $args );
                echo '<ul>';
                while ( $loop->have_posts() ) : $loop->the_post();
                    echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a><li>';
                endwhile;
                echo '</ul>';

                wp_reset_postdata();

I've managed to get the list, but it has 3 problems:

  • It's not limiting to 5 posts
  • It's duplicating the li elements (1 correct element, 1 empty element)
  • It's showing the current post also on the list (I only want to show the other posts with the same term, not the current post)

For example, I get this on one of the posts:

enter image description here

1 Answer 1

2
  • It's not limiting to 5 posts

That's because the posts_per_page arg should actually be in the same level as the post_type arg and not put inside the tax_query array. I.e.

$args = array(
    'post_type'      => 'oferta',
    'posts_per_page' => 5, // add it here
    'tax_query'      => array(
        array(
            'taxonomy'       => 'categoria',
            'posts_per_page' => 5, // NOT here
            ...
        ),
    ),
);
  • It's duplicating the li elements (1 correct element, 1 empty element)

That's very likely because you didn't close the elements, i.e. you forgot the closing </li> tag and instead (mistakenly) used <li>. So make sure to properly close your li tag:

echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';  // correct - use </li>
//echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a><li>'; // incorrect
  • It's showing the current post also on the list (I only want to show the other posts with the same term, not the current post)

You could use the post__not_in arg, i.e. add 'post__not_in' => array( get_the_ID() ), to your $args array, but NOT IN queries are known to cause performance issues (just like 'orderby' => 'rand'), so as an alternate way, you can instead use PHP to ignore the current post, but you should set the posts_per_page to 6 since we're not excluding the current post at the database level.

Working example:

// Assign the current post ID to a variable.
$current_post_id = get_the_ID();

// Make the custom posts query.
$loop = new WP_Query( $args );

// Post counter; if the value has reached 5, then we exit the `while` loop below.
$counter = 0;

// Display the posts.
echo '<ul>';
while ( $loop->have_posts() ) {
    if ( $counter >= 5 ) {
        break;
    }

    $loop->the_post();
    // Display the post if it's not the current post with the ID $current_post_id
    if ( $current_post_id !== get_the_ID() ) {
        echo '<li>your HTML/code here..</li>';
        $counter++;
    }
}
echo '</ul>';
2
  • 1
    Works perfectly. Thank you very much!! :)
    – migueltic
    Aug 18, 2022 at 8:44
  • You're welcome :) And I thought I should add that if you don't need pagination (or to paginate the results), then you should add 'no_found_rows' => true to your $args because doing so can improve performance.
    – Sally CJ
    Aug 18, 2022 at 13:52

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.