Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Experiments in skipping the main WP query
<?php
add_action( 'setup_theme', function () {
global $wp;
class RM_FakeWP extends WP {
public $skip_query = false;
function main($query_args = '') {
$this->init();
$this->parse_request($query_args);
$this->send_headers();
if ( ! $this->skip_query ) {
$this->query_posts();
$this->handle_404();
}
else {
$this->build_query_string();
global $wp_the_query;
$wp_the_query->init();
$wp_the_query->query = $wp_the_query->query_vars = wp_parse_args( $this->query_vars );
$wp_the_query->posts = array();
$wp_the_query->request = 'SELECT FROM nonexistent WHERE 0=1';
$wp_the_query->parse_query();
$fake_post = new WP_Post( new stdClass );
$fake_post->filter = 'raw';
$wp_the_query->posts = array( $fake_post );
$wp_the_query->post_count = 0;
$wp_the_query->post = $fake_post;
$GLOBALS['post'] = $fake_post;
}
$this->register_globals();
/**
* Fires once the WordPress environment has been set up.
*
* @since 2.1.0
*
* @param WP &$this Current WordPress environment instance (passed by reference).
*/
do_action_ref_array( 'wp', array( &$this ) );
}
}
$wp = new RM_FakeWP();
$wp->skip_query = apply_filters( 'rm_skip_main_query', false );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment