Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#14869 closed enhancement (wontfix)

Adding a "callback" to WP_Query()

Reported by: mikeschinkel Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords: close
Focuses: Cc:


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)

#1 @filosofo
9 years ago

The current WP_Query object is passed to the posts_request filter, so why can't you just access its query_vars property inside the callback attached to posts_request?

#2 @scribu
9 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(' ... ');

#3 @scribu
9 years ago

Of course, you can use a class to encapsulate this, instead of a global variable.

#4 @scribu
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.