[wp-hackers] Skip Main Query
Andy Skelton
skeltoac at gmail.com
Sat Jul 31 11:48:11 UTC 2010
> No I've noticed that WordPress still queries the posts from the start page. Is there a way to avoid that? Since I don't need the posts, I don't want to make that query.
Just replace wp::query_posts() with something more appropriate for
your situation. Easy. ;-)
This is how I made the Freshly Pressed feed on WordPress.com. It's a
multisite feed that gets blog_id and post_id from a table. It
populates $wp_the_query with all of the values necessary for a loop,
avoiding the standard core query, and switching blogs on each post.
This code is from the theme's functions.php. It could be written as a
plugin as well. There are probably some useless or erroneous lines. I
haven't planned on refining or packaging this, so consider this its
GPL distribution. :) If you decide to make an official plugin from
this, please add "andy" and "automattic" as contributors.
add_action('init', 'freshlypressed_init');
// Runs at end of init. Supplants global WP object.
function freshlypressed_init() {
$GLOBALS['wp'] =& new freshlypressed_wp();
add_action('the_post', 'freshlypressed_the_post', 10, 1);
add_action('loop_end', 'freshlypressed_the_post', 10, 1);
add_filter('get_lastpostmodified', 'freshlypressed_latest', 10001);
}
// This gets the latest date from the right place so feed 304s work
properly.
function freshlypressed_latest() {
global $wpdb;
return $wpdb->get_var("SELECT picked_on FROM botm_en ORDER BY
picked_on DESC LIMIT 1");
}
// This runs before every loop and after the last one. It switches to
the post's blog for each post in the loop.
function freshlypressed_the_post($post = null) {
restore_current_blog();
if ( is_object($post) && isset($post->blog_id) )
switch_to_blog($post->blog_id);
}
class freshlypressed_wp extends wp {
function query_posts() {
global $wp_the_query, $wpdb;
$this->build_query_string();
// $wp_the_query->query($this->query_vars);
// The whole point of overriding this method is to replace that
// commented third line with our post-gathering code
(below).
$wp_the_query->parse_query($this->query_vars);
// Nothing but feeds here. And none of those "singular" comment
feeds. Just paged feeds.
if ( !is_feed() || is_singular() ) {
wp_redirect('/feed/');
die;
}
if ( isset($this->query_vars['paged']) )
$page = max(intval($this->query_vars['paged']), 1);
else
$page = 1;
$start = 10 * ($page - 1);
$blog_posts = $wpdb->get_results("SELECT
SQL_CALC_FOUND_ROWS blog_id, post_id FROM botm_en ORDER BY picked_on
DESC LIMIT $start, 10");
$wp_the_query->found_posts = $wpdb->get_var("SELECT FOUND_ROWS()");
$wp_the_query->max_num_pages =
ceil($wp_the_query->found_posts / 10);
$wp_the_query->post_count = 10;
foreach ( $blog_posts as $a ) {
$post =
sanitize_post(get_blog_post($a->blog_id, $a->post_id), 'raw');
$post->blog_id = $a->blog_id;
$wp_the_query->posts[] = $post;
}
$wp_the_query->post = $wp_the_query->posts[0];
if ( is_home() ) {
$wp_the_query->is_home = null;
$wp_the_query->is_archive = true;
}
}
}
More information about the wp-hackers
mailing list