Created
September 12, 2012 04:26
-
-
Save moimikey/3704281 to your computer and use it in GitHub Desktop.
super fast random WordPress tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return a random post tag from the wp_terms table | |
* | |
* average 0.4ms on a 3K table | |
* | |
* @author Michael Scott Hertzberg | |
* @return mixed|string | |
*/ | |
function m_random_tag() { | |
global $wpdb; | |
return $wpdb->get_var( $wpdb->prepare( 'SELECT name FROM wp_terms JOIN( SELECT CEIL(RAND() * (SELECT MAX(term_id) FROM wp_terms)) AS term_id) AS m USING( term_id );' ) ); | |
} |
benchmark comparison to order rand, my sql statement is a lot quicker
Can you do a fast random post for wordpress, I have been trying for the past 3 hours, but they all take longs on my database (4k records)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Afaik there's a
'order' => 'rand'
arg by default inget_terms()
.