Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use posts_pre_query filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 12, 2022
5 minutes read
Share Via:

posts_pre_query filter

This filter lets you change the posts that WordPress shows by default. Every time you open a page in WordPress, it runs a default query to fetch a set of posts. For instance, on the homepage, it fetches the latest posts, and on archive pages, it pulls posts from a specific month, category, or tags. The posts_pre_query filter allows you to tweak these default behaviors and customize how WordPress displays posts according to your preferences.

To use posts_pre_query filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins, always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_posts_pre_query_defaults which takes 2 parameters and we registered using add_filter. The first parameter posts_pre_query is name of the hook, The second parameter modify_posts_pre_query_defaults is name of the function which need to be called, third parameter is the priority of calling the hook if same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometime, you have to remove a registered hook so you can use remove_filter to remove posts_pre_query filter.

Parameters

    Below are the 2 parameters are required to use this hook.

  • $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).

Live Examples

                        function modify_posts_pre_query_defaults($posts, $query) { 
   
                            // Update the $posts variable according to your website requirements and return this variable. You can modify the $posts variable conditionally too if you want.

                            return $posts; 
                        }
                        // add the filter
                        add_filter( "posts_pre_query", "modify_posts_pre_query_defaults", 10, 2 );

Example 1: Showcase Only Custom Post Types on Homepage

function customize_homepage_query($query) {
    if (is_home() && $query->is_main_query()) {
        $query->set('post_type', array('your_custom_post_type'));
    }
}
add_action('posts_pre_query', 'customize_homepage_query');

Example 2: Exclude a Category from Archive Page

function exclude_category_from_archive($query) {
    if ($query->is_archive() && $query->is_main_query()) {
        $query->set('cat', '-5'); // Replace 5 with the ID of the category you want to exclude
    }
}
add_action('posts_pre_query', 'exclude_category_from_archive');

Example 3: Filter Posts Based on User Role

function filter_posts_by_user_role($query) {
    if (is_admin() && $query->is_main_query()) {
        global $current_user;
        $user_roles = $current_user->roles;
        
        // Show only specific post types based on user role
        if (in_array('editor', $user_roles)) {
            $query->set('post_type', array('your_custom_post_type'));
        }
    }
}
add_action('posts_pre_query', 'filter_posts_by_user_role');

To remove a hook callback, use the example below.

remove_filter( "posts_pre_query", "modify_posts_pre_query_defaults", 10, 2 );

Please make sure provide the same callback function name, priority and number of arguments while removing the hook callback.

WePlugins (formerly Flipper Code) is a Premium WordPress Plugins development company and integrating new functionalites into WordPress sites in form of custom WordPress Plugins since 2012. If you’re having any trouble using this hook, please contact our WordPress Development Team and we’d be happy to assist you.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.