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->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: