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 have an ajax request that returns the result of a WP_Query loop.

I would like to detect in pre_get_posts filter the specific wp_query instance that is used in the ajax handler.

I thought placing a custom parameter in the $args of the WP_Query in the ajax handler:

$args= array(...,
            'ajax' => 'AJAX',//custom param for detection of specific WP_Query instance
            ....);

and by this way detecting the specific instance in pre_get_post:

 if(isset($query->query_vars['ajax']){
    Do stuff
 }

I do not know if this has any sense to you...

Thanks¡¡

share|improve this question
    
If you mean like this (look for my_variable), then yes, that works. –  s_ha_dum Jul 3 '13 at 18:37
    
thanks I meant that, the only issue that worried me after reading your response was : "I do not know if passing additional variables like that is by design, so be aware that that may be unofficial behavior." Can any proble arise in future versions of WP? –  mcartur Jul 4 '13 at 6:59
add comment

1 Answer

up vote 1 down vote accepted

You can access query variables (including custom ones) via the WP_Query::get() method.

For example:

$my_query = new WP_Query( array(
      ...
      'wpse105219_custom_var' => 'foobar',
      ...
) );

To 'catch' this at pre_get_posts:

add_action( 'pre_get_posts', 'wpse105219_pre_get_posts', 10 );
function wpse105219_pre_get_posts( $query ){
      if( $query->get( 'wpse105219_custom_var' ) == 'foobar' ){
         //Alter $query
      }
}
share|improve this answer
    
Thank you, I suppose that this practice is accepted by wp... –  mcartur Jul 4 '13 at 7:00
    
Yes, its fine to use this. If you want the custom variable to be extracted from the url, e.g. www.mysite.com?wpse105219_custom_var=foobar then you need to add it via the query_vars filter: wordpress.stackexchange.com/questions/20946/… –  Stephen Harris Jul 4 '13 at 8:28
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.