Joey
(@leglesslizard)
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() 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() to create a link within your html.
Hope this helps!
Regards,
Joey
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
(@leglesslizard)
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.
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
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)
<?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
(@leglesslizard)
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
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
(@leglesslizard)
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
wp_get_post_terms – may also be helpful.
Good luck π
I’ll try to figure it out, thanks for all your help Joey π