Opened 15 years ago
Closed 15 years ago
#14869 closed enhancement (wontfix)
Adding a "callback" to WP_Query()
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Query | Keywords: | close |
Focuses: | Cc: |
Description
As WordPress apps get more complex with many different post types and taxonomies it makes it harder to write robust plugins when using global filters on WP_Query(). Instead of writing code and encapsulating you end up writing complex if expressions in hopes to keep everything else from triggering your SQL additions in your post_where
and other hooks.
What is the chance of adding a callback filter to the $args for WP_Query() that would be called after the posts_request filter so that we don't have to depend on global filters when we need to modify a query for a specific call in a plugin? (Ideally this callback would work for all the filters in WP_Query() but I'd settle for one that gets called after posts_request vs. not having any.)
This actually is a much larger issues than a single callback can fix, but a single callback would at least be a good start.
Change History (4)
#2
@
15 years ago
- Keywords close added
I agree with filosofo. Here's a snippet that practically does what you're asking for:
function my_posts_where( $where, $wp_query ) { if ( $wp_query !== $GLOBALS['my_wp_query'] ) return $where; // this will only execute for $my_wp_query } add_filter('posts_where', 'my_posts_where', 10, 2); global $my_wp_query; $my_wp_query = new WP_Query(' ... ');
The current WP_Query object is passed to the
posts_request
filter, so why can't you just access itsquery_vars
property inside the callback attached toposts_request
?