[wp-hackers] Any way to bypass $wp->query_posts()?

Mike Schinkel mikeschinkel at newclarity.net
Wed Apr 8 18:07:16 GMT 2009

"Otto" <otto at ottodestruct.com> wrote:
> Sure there is. In order to get the Page, you have to run 
> the query on wp_posts. I assume you're using some internal 
> tag in the post_content to have it know which page to 
> display the tweets on, after all. 

Thanks for the reply.

Yes you'd be right when making all the standard assumptions for a WordPress site. However, I'm writing a plugin that bypasses those assumptions. 

BTW, when you say "internal tag" what specifically do you mean? A pagename? An actual tag? Something else?

> Of course, you could bypass that entirely and then use 
> a page template instead, but even then you need to 
> query the posts table in order to know which page 
> template is being used.

Actually, I'm bypassing page templates completely, using template_redirect, and setting new rules in WP_Rewrite. (And Chris Jean recently battled[1] with this same type of thing so it is not just me.) 

My plugin allows a themer or plugin developer to define any arbitrary URL they want to have processed and have it routed to run whatever they want to run. Basically it lets a themer or plugin dev start a page with a (mostly) clean slate.  With this plugin and in some use-cases running get_posts() makes no sense because the process flow never makes it to running a page template or even any of the "if" logic template-loader.php.

To address these use-cases there is simply the need to bypass running the query in /wp-include/classes.php (I added the one line "if (!$this->bypass_query)"):

	function main($query_args = '') {
		if (!$this->bypass_query)
		do_action_ref_array('wp', array(&$this));

BTW, one thing I am finding is that the concept of using WP_Rewrite is far from fleshed-out. There are many places in WordPress where the "type" of a URL is hardcoded in implicit fashion. The most recent example I found was in wp_title(). wp_title() makes a bunch of assumptions but doesn't give the 'wp_title' hook any context to know what it's operating on so you can't really generate an appropriate title for your URL. Thus even though people on this list say "The right way to do handle custom URLs is via WP_Rewrite" the reality is that logic path has never been tested and has many issues. I'd like to eventually address all of those by providing a "custom URL" escape valve in all relevant contexts, but for now I'd like to just be able to bypass running the query.

-Mike Schinkel
Custom Wordpress Plugins

[1] http://www.nabble.com/Showing-custom-page-types-td21733174.html

More information about the wp-hackers mailing list