Skip to content

Instantly share code, notes, and snippets.

@paulgibbs
Created April 25, 2013 14:24
Show Gist options
  • Save paulgibbs/2150f1d778ae5d0876de to your computer and use it in GitHub Desktop.
Save paulgibbs/2150f1d778ae5d0876de to your computer and use it in GitHub Desktop.
<?php
/**
* Get random posts; an alternate approach for dealing with large tables.
*
* MySQL queries that use ORDER BY RAND() can be pretty challenging and slow on large datasets.
* This function is an alternative method for getting random posts, though it's still very very inefficent.
* You probably don't want to use this function.
*
* Override the $vip_get_random_post_ids_where_add global to add your own WHERE condition:
* $vip_get_random_post_ids_where_add = "AND post_status='publish' AND post_type='post' AND post_date_gmt > '2009-01-01 00:00:00';
*
* You'll want to enable caching and avoid querying all posts by adding the following action:
* add_action( 'save_post', 'vip_refresh_random_posts_all_ids', 1 ); // add this to functions.php
*
* @author tottdev
* @param int $amount Optional. Amount of random posts to get. Default 1.
* @param bool $return_ids Optional. To just get the IDs, set this to true, otherwise post objects are returned (the default).
* @return array
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment