pre_get_posts

Fires after the query variable object is created, but before the actual query is run.

Description

do_action( 'pre_get_posts', (array) $array ); 

Note: If using conditional tags, use the method versions within the passed instance (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions like is_main_query() test against the global $wp_query instance, not the passed one.

Parameters (1)

0. $array (array) => array( &$this )
&$this The WP_Query instance (passed by reference).

Usage

To run the hook, copy the example below.
  1. // run the action 
  2. do_action( 'pre_get_posts', $array ); 
The following example is for adding a hook callback.
  1. // define the pre_get_posts callback 
  2. function action_pre_get_posts( $array ) { 
  3. // make action magic happen here... 
  4. }; 
  5.  
  6. // add the action 
  7. add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 ); 
To remove a hook callback, use the example below.
  1. // remove the action 
  2. remove_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 ); 

Defined (1)

The action is defined in the following location(s).

/wp-includes/query.php  
  1. do_action_ref_array( 'pre_get_posts', array( &$this ) );