Filter pre_get_posts
is used for modifying the query from theme or plugin. This hook is called after the query variable object is created, but before the actual query is run.
Codex documentation of pre_get_posts
Although this filter is very handy and useful, we need to be very careful using it. We should very careful about targeting the query. This filter is applied for both front end and back end admin panel. WordPress provides several conditional function which can be used to target the specific page.
- is_main_query()
- is_admin()
- is_home()
- is_post_type_archive()
- and many more
See here for more conditional tags
Example, if you want to target only home page then you can use like this:
function my_custom_function( $query ) {
if ( $query->is_home() && $query->is_main_query() && !is_admin() ) {
//change query parameter here for home page
}
}
add_action( 'pre_get_posts', 'my_custom_function' );