September 15, 201313 yr Struggling to get this working as desired. I am using the code based on http://www.hongkiat....ithout-plugins/ Modified slightly to display posts in random order, and from a custom post type. <?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), 'post_type' => 'events', 'posts_per_page'=>4, 'orderby' => 'rand' ); $my_query = new wp_query( $args ); while( $my_query->have_posts() ) { $my_query->the_post(); ?> <div class="relatedthumb"> <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br /> <?php the_title(); ?> </a> </div> <? } } $post = $orig_post; wp_reset_query(); ?> Now, that is kind of working. It is displaying 4 posts from the posttype 'events', and, when refreshed, it displays in a random order.However, I was expecting it to display 4 random posts from the entirepost type (10+ posts). All the posts have the same tag, but it seems toonly display the first 4. Any ideas anybody?
September 15, 201313 yr orderby orders the retrieved result, not the query itself. order is used to order the query. That said, using RAND() in a SQL statement is slow. It might be best for you to retrieve the entire list, and use a count to limit the amount you use. Such as: <?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), 'post_type' => 'events', 'posts_per_page' => -1, 'orderby' => 'rand' ); $my_query = new wp_query( $args ); $i = 0; while( $my_query->have_posts() && $i <4 ) { $my_query->the_post(); $i++; ?> <div class="relatedthumb"> <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br /> <?php the_title(); ?> </a> </div> <? } } $post = $orig_post; wp_reset_query(); ?>There may well be better ways around this, however.
September 16, 201313 yr Author orderby orders the retrieved result, not the query itself. order is used to order the query. That said, using RAND() in a SQL statement is slow. It might be best for you to retrieve the entire list, and use a count to limit the amount you use. Such as: <?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), 'post_type' => 'events', 'posts_per_page' => -1, 'orderby' => 'rand' ); $my_query = new wp_query( $args ); $i = 0; while( $my_query->have_posts() && $i <4 ) { $my_query->the_post(); $i++; ?> <div class="relatedthumb"> <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br /> <?php the_title(); ?> </a> </div> <? } } $post = $orig_post; wp_reset_query(); ?>There may well be better ways around this, however. Hi Andy, that just returns the same as my original code?
September 16, 201313 yr Author Have now tried about 20 different code snippets, and every one isonly showing the latest 4 posts in that post type. Yes it randomises theorder, but it wont show older posts.... Any ideas?
September 16, 201313 yr Author To add, it works with normal posts fine, it will pull in 4 completely random posts, but with CPT's it only pulls in the most recent 4...
September 16, 201313 yr Author FIXED! My original code now works. There was a conflict between that and a plugin I was using, http://wordpress.org/plugins/post-types-order/. Thanks anyway Andy.
Create an account or sign in to comment