- This topic has 5 replies, 2 voices, and was last updated 4 years, 4 months ago by
Tom.
-
AuthorPosts
-
January 5, 2019 at 7:46 am #773870
eduard sans
Hi! I would like to know how to filter the search results of a related posts module I have introduced in the file single.php. Right now it always display the same posts (which I assume are the last posts I submitted). Considering the code below, I would like to be able to do two things (separately in different modules):
1) A related posts that shows posts filtered by the category or tag I write down.
2) A related posts module that shows posts randomly (no matter what category or tag).
Any ideas? Thanks in advance!
<div class="relatedposts full-width"> <h3>También te puede interesar...</h3> <div class="posts-container"> <?php $orig_post = $post; global $post; $tags = wp_get_post_tags($post->ID); if ($tags) { $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; $args=array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'posts_per_page'=>4, // Number of related posts to display. 'caller_get_posts'=>1 ); $my_query = new wp_query( $args ); while( $my_query->have_posts() ) { $my_query->the_post(); ?> <div class="relatedthumb"> <div class="relatedimg"> <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(250,200)); ?></a> </div> <div class="related-title-box"> <a class="related-title" href="<? the_permalink()?>"><?php the_title(); ?> </a> </div> </div> <? } } $post = $orig_post; wp_reset_query(); ?> </div> </div> <div class="comments-area"> <?php comments_template(); ?> </div> <?php endif; endwhile; /** * generate_after_main_content hook. * * @since 0.1 */ do_action( 'generate_after_main_content' ); ?> </main><!-- #main --> </div><!-- #primary --> <?php /**
January 5, 2019 at 4:58 pm #774127Tom
Lead DeveloperLead DeveloperHi there,
You could try replacing your args with this:
$args=array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'posts_per_page'=>4, // Number of related posts to display. 'caller_get_posts'=>1, 'orderby' => 'rand' );
January 5, 2019 at 11:55 pm #774229eduard sans
thanks tom! that works for random posts! but I would like to know also how to display random posts of a certain category or tag, under what line can i write down the category or tag that I want it to display?
January 6, 2019 at 9:14 am #774612Tom
Lead DeveloperLead DeveloperYou can add the category to the list of args: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
$args=array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'posts_per_page'=>4, // Number of related posts to display. 'caller_get_posts'=>1, 'orderby' => 'rand', 'cat' => 10 );
January 6, 2019 at 1:30 pm #774742eduard sans
Thanks again Tom! and thanks for the link to the wordpress codex. I didn’t even know there was such a useful page, definitely going to read it.
January 6, 2019 at 5:45 pm #774870Tom
Lead DeveloperLead DeveloperYou’re welcome 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.