WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

    Do stuff

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


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
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

Your Answer


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.