The simplest way is to add the action right before the query and remove it immediately after.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // tell WordPress about our new query var function wpse52480_query_vars( $query_vars ){ $query_vars[] = 'my_special_query'; return $query_vars; } add_filter( 'query_vars', 'wpse52480_query_vars' ); // check if our query var is set in any query function wpse52480_pre_get_posts( $query ){ if( isset( $query->query_vars['my_special_query'] ) ) // do special stuff return $query; } add_action( 'pre_get_posts', 'wpse52480_pre_get_posts' ); |
and in the template:
1 2 3 4 5 6 | // set the query var (along with whatever others) to trigger the filter $args = array( 'my_special_query' => true ); $my_secondary_loop = new WP_Query( $args ); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.