Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Custom Post Type - Show related posts based on tag

Featured Replies

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 post
type 'events', and, when refreshed, it displays in a random order.
However, I was expecting it to display 4 random posts from the entire
post type (10+ posts). All the posts have the same tag, but it seems to
only display the first 4.


 


Any ideas anybody?

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.
  • 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?

  • Author

Have now tried about 20 different code snippets, and every one is
only showing the latest 4 posts in that post type. Yes it randomises the
order, but it wont show older posts....

 

Any ideas?

  • 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...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.