pre_get_posts
Fires after the query variable object is created, but before the actual query is run.
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.
Usage
add_action( 'pre_get_posts', 'wp_kama_pre_get_posts_action' ); /** * Function for `pre_get_posts` action-hook. * * @param WP_Query $query The WP_Query instance (passed by reference). * * @return void */ function wp_kama_pre_get_posts_action( $query ){ // action... }
- $query(WP_Query)
- The WP_Query instance (passed by reference).
Changelog
Since 2.0.0 | Introduced. |
Where the hook is called
pre_get_posts
wp-includes/class-wp-query.php 1881
do_action_ref_array( 'pre_get_posts', array( &$this ) );
Where the hook is used in WordPress
wp-includes/block-template.php 16
add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
wp-includes/block-template.php 342
remove_filter( 'pre_get_posts', '_resolve_template_for_new_post' );