Quantcast
Channel: The Blog of Brady J. Frey
Viewing all articles
Browse latest Browse all 117

Randomizing Post Data in WordPress 2.5

$
0
0

I recently updated a client’s business website to WordPress 2.5, and noticed the dated rand_posts trick failed me. Further investigation noted that WordPress had completely changed it’s table schema from categories, post2cat, and link2cat tables to a more flexible Taxonomy Schema.

After a bit of Googling, the WordPress forum linked me to the new documentation, noting that random posts are a lot simpler now with the MySQL RAND() function. Previous code for MacCentric was:

global $wpdb; $numposts = 1; $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = 'publish' and $wpdb->categories.cat_name = 'Customer Quotes' ORDER BY RAND() LIMIT $numposts"); foreach($rand_posts as $post) : setup_postdata($post);

…and became:

$rand_posts = get_posts('numberposts=1&category=4&orderby=RAND()'); foreach( $rand_posts as $post ) : setup_postdata($post);

Something to keep in mind for the designers rebuilding their templates;)


Viewing all articles
Browse latest Browse all 117

Trending Articles