Title: Random Post from Custom Post Type
Last modified: August 22, 2016

---

# Random Post from Custom Post Type

 *  [needhelpwithnimble](https://wordpress.org/support/users/needhelpwithnimble/)
 * (@needhelpwithnimble)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/)
 * My website [http://www.animewolf.tv](http://www.animewolf.tv) has a bunch of 
   series for different Anime’s with embeded players.
 * Each series is part of a custom post type, here is an example on one.
 * [http://animewolf.tv/series/hacksign/](http://animewolf.tv/series/hacksign/)
 * If you look on
 * [http://animewolf.tv/watch-anime/](http://animewolf.tv/watch-anime/)
 * it has a list of all of the shows.
 * I want to generate a button with a ‘surprise me’ so when people click on it, 
   a random show will be displayed.
 * For example, if you click on it you could get..
 * [http://animewolf.tv/series/hacksign/](http://animewolf.tv/series/hacksign/)
 * then again you would get
    [http://animewolf.tv/series/dragonball/](http://animewolf.tv/series/dragonball/)
 * then again
 * [http://animewolf.tv/series/death-note/](http://animewolf.tv/series/death-note/)
 * etc..
 * any one have any idea how to do this or if I can get a plugin for it?
 * EDIT:
    What I would like can be shown here:
 * [http://www.animeseason.com/](http://www.animeseason.com/)
 * (Left hand side, where it says ‘Random Anime’)
 * Would be super appreciative. thx

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Joey](https://wordpress.org/support/users/leglesslizard/)
 * (@leglesslizard)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550595)
 * Hey,
 * how about dropping a little code like this somewhere (changing post_type to your
   custom post type)?
 *     ```
       <?php $posts = get_posts( array( 'orderby' => 'rand', 'numberposts' => 1, 'post_type' => 'post' ) );
       foreach($posts as $post) : ?>
         <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Random Post</a></p>
       <?php endforeach; ?>
       ```
   
 * Could go directly into sidebar.php for example but would be better to create 
   a child theme and/or your own template so you don’t lose it on update 🙂
 * All this does is uses wordpress’ built in [get_posts()](http://codex.wordpress.org/Template_Tags/get_posts)
   function to return posts of the stated post type in a random order and numberposts
   => 1 ensures only one is returned. You can then use [the_permalink()](http://codex.wordpress.org/Function_Reference/the_permalink)
   to create a link within your html.
 * Hope this helps!
    Regards, Joey
 *  Thread Starter [needhelpwithnimble](https://wordpress.org/support/users/needhelpwithnimble/)
 * (@needhelpwithnimble)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550596)
 * Hi Joey, Thanks but that didn’t help me too much.
 * I’ve managed to narrow it down a bit, the following code will work to some extent:
 * `
    $randomPost = get_posts(array( ‘post_type’ => ‘anime’, ‘numberposts’ => 1,‘
   orderby’ => ‘rand’ ));
 *  foreach($randomPost as $post) (
    wp_redirect(get_permalink($post->ID)) );
 * But all it will do is display a random episode, which is on the right track, 
   but I want it to display a random series, I don’t know how to do that!
 *  [Joey](https://wordpress.org/support/users/leglesslizard/)
 * (@leglesslizard)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550599)
 * So each episode is a post of post_type ‘anime’?
 * How are your series setup? You said before that they are a custom post type. 
   If that is the case then you need to change anime to series (or whatever the 
   name of that post type is) and it will return a random post of that type.
 * Apologies if I have misunderstood.
 *  Thread Starter [needhelpwithnimble](https://wordpress.org/support/users/needhelpwithnimble/)
 * (@needhelpwithnimble)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550601)
 * I’ll try to link it, as it’s difficult to explain seeing as I’m so confused myself,
   but here it goes.
 * [http://puu.sh/diTuX/3b57863985.png](http://puu.sh/diTuX/3b57863985.png)
 * I’m not actually too sure what controls the series =\ nor am I sure how to find
   out what does (I’m still fairly new to WP)
 *  Thread Starter [needhelpwithnimble](https://wordpress.org/support/users/needhelpwithnimble/)
 * (@needhelpwithnimble)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550608)
 *     ```
       <?php
       /* Template Name: Random Post */
       global $post;
   
       $args = array(
   
       $randomPost = get_posts(array(
       	'numberposts' => 1,
       	'orderby' => 'rand',
       	'get_term_by' => 'anime',
       	'tax_query'=> array(
       				'taxonomy' => 'series',
       				'slug' => 'series',
       				'term' => 'series'
       		)
       	)
       ));
   
       foreach($randomPost as $post) (
       	wp_redirect(get_permalink($post->ID))
       );
       ```
   
 * What I’m working with now, probably so off but I just don’t know
 *  [Joey](https://wordpress.org/support/users/leglesslizard/)
 * (@leglesslizard)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550611)
 * Series is a custom taxonomy so we want to look at get_terms().
 * I’ve tried to recreate a very simple version on my site and this code works for
   me, hopefully your setup is the same with that plugin or at least similar enough
   for you to have a decent starting point (quite tired so ignore the messy code!):
 *     ```
       $terms = get_terms( 'series' );
       $rand = array_rand( $terms );
       $random_term = array();
       $random_term[] = $terms[$rand];
   
       foreach ( $random_term as $term ) {
           $term_link = get_term_link( $term );
   
           // If there was an error, continue to the next term.
           if ( is_wp_error( $term_link ) ) {
               continue;
           }
           echo '<p><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></p>';
       }
       ```
   
 * Hopefully that’ll get you started 🙂 good luck!
    Regards
 *  Thread Starter [needhelpwithnimble](https://wordpress.org/support/users/needhelpwithnimble/)
 * (@needhelpwithnimble)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550618)
 * That’s so close, it’s displaying series titles!
 * I just need the entire series now, almost as if it would just redirect to a new
   series how the function for the ‘post_type’ => ‘anime’ would work
 *  [Joey](https://wordpress.org/support/users/leglesslizard/)
 * (@leglesslizard)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550622)
 * I’m sorry I’m unsure how to replicate that setup on my end. It’s hard for me 
   to create the code without the plugin installed :p
 * You can have a scan through the codex for those functions and see if anything
   comes close to what you need:
    [get_terms](http://codex.wordpress.org/Function_Reference/get_terms)
   [wp_get_post_terms](http://codex.wordpress.org/Function_Reference/wp_get_post_terms)–
   may also be helpful.
 * Good luck 🙂
 *  Thread Starter [needhelpwithnimble](https://wordpress.org/support/users/needhelpwithnimble/)
 * (@needhelpwithnimble)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550624)
 * I’ll try to figure it out, thanks for all your help Joey 🙂

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Random Post from Custom Post Type’ is closed to new replies.

## Tags

 * [cpt](https://wordpress.org/support/topic-tag/cpt/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [redirect](https://wordpress.org/support/topic-tag/redirect/)

 * 10 replies
 * 2 participants
 * Last reply from: [needhelpwithnimble](https://wordpress.org/support/users/needhelpwithnimble/)
 * Last activity: [11 years, 5 months ago](https://wordpress.org/support/topic/random-post-from-custom-post-type/#post-5550624)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
