Retrieve the url of a random post several times
I’m trying to retrieve several times the url of a random published post, but I want to limit the number of Mysql queries. So my idea was to retrieve all the published posts at the beginning of the script and then pick a random post ID from this when I need it. I tried:
$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' "; $request .= "AND post_date_gmt < '$now' ORDER BY RAND()"; $posts = $wpdb->get_results($request);
and the end of my script would be something like
$url = get_permalink($post_id);
but I don’t know how to get the random post ID $post_id from the data I’ve retrieved. Can someone help me?
- The topic ‘Retrieve the url of a random post several times’ is closed to new replies.