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.


Top ↑

Parameters

$posts

(WP_Post[]|int[]|null)Return an array of post data to short-circuit WP's query, or null to allow WP to run its normal queries.

$query

(WP_Query)The WP_Query instance (passed by reference).


Top ↑

Source

File: wp-includes/class-wp-query.php

View on Trac



Top ↑

Changelog

Changelog
VersionDescription
4.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More