posts_pre_query WordPress Filter Hook
The posts_pre_query hook is one of the most important hooks in WordPress. It is called before the query is sent to the database. This hook allows you to modify the query before it is executed. You can use this hook to make sure that only certain posts are returned from the database. For example, you can use this hook to make sure that only posts from a certain category are returned. This hook is also very useful for debugging. If you are having trouble with a query, you can use this hook to print out the SQL query that is being executed.
apply_filters_ref_array( 'posts_pre_query', WP_Post[]|int[]|null $posts , WP_Query $query ) #
Filters the posts array before the query takes place.
Description
Return a non-null value to bypass WordPress’ default post queries.
Filtering functions that require pagination information are encouraged to set the found_posts
and max_num_pages
properties of the WP_Query object, passed to the filter by reference. If WP_Query does not perform a database query, it will not have enough information to generate these values itself.
Parameters
Source
Changelog
Version | Description |
---|---|
4.6.0 | Introduced. |