Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've build a theme using custom post-types with queries like

$my_custom_post_type_query = new WP_Query( array( 'my_arg' => 'value' ));

Now I'm writing a plugin where I need to do something like

$wp_query->get_queried_object_id();

but for $my_custom_post_type_query instead of $wp_query.

How can I (from my plugin file)

  1. check if there are any custom queries, and if so

  2. call a specific custom query within my plugin?

Any ideas much appreciated!

share|improve this question

1 Answer 1

I don't really understand what you're trying to do, so here goes nothing:

There are a lot of hooks within WP_Query. Most of them pass the current instance as one of the arguments. So, here's how you could check for a "custom query":

function check_wp_query( $posts, $wp_query ) {
  if ( $wp_query->get('my_arg') ) {
    // it's a custom query
  }

  return $posts;
}
add_filter( 'the_posts', 'check_wp_query', 10, 2 );

The trick is that each custom query has to be identifiable by at least one of it's query vars.

share|improve this answer
    
Would this also work if my query isn't named $wp_query but $my_custom_post_type_query? Like function check_wp_query( $posts, $my_custom_post_type_query ) { if ( $my_custom_post_type_query->get('my_arg') ) { // it's a custom query } return $posts; } add_filter( 'the_posts', 'check_wp_query', 10, 2 ); Thanks! –  user3865 Mar 11 '11 at 0:05
    
Sorry, I have to find out how formatting works in comments… :-/ –  user3865 Mar 11 '11 at 0:11
    
Bang, the last comment crashed my browser AND my user account on Mac OSX. Interesting. Thanks a bunch anyway, Scribu. I'll go find out about those hooks before I ask more stupid questions. ;-) –  user3865 Mar 11 '11 at 0:20
1  
It's a formal argument, so you can call it whatever you want. Not to be confused with global $wp_query. –  scribu Mar 11 '11 at 0:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.