This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
posts_request_ids filter
Filters the Post IDs SQL request before sending.
To use the `posts_request_ids` filter, first, you have to register it using `add_filter`. You can write this code into the `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 `weplugins_modify_posts_request_ids_defaults` which takes 2 parameters and we registered using `add_filter`. The first parameter `posts_request_ids` is the name of the hook, the second parameter `weplugins_modify_posts_request_ids_defaults` is the name of the function that needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometimes, you have to remove a registered hook so you can use `remove_filter` to remove `posts_request_ids` filter.
Parameters
- $request : (string) The post ID request.
- $query : (WP_Query) The WP_Query instance.
Below are the 2 parameters required to use this hook.
Live Example
apply_filters( 'posts_request_ids', string $request, WP_Query $query )
Below is an example of how you can use this hook.
function weplugins_modify_posts_request_ids_defaults($request, $query) { // Update the $request variable according to your website requirements and return this variable. You can modify the $request variable conditionally too if you want. return $request; } // add the filter add_filter( "posts_request_ids", "weplugins_modify_posts_request_ids_defaults", 10, 2 );
To remove a hook callback, use the example below.
remove_filter( "posts_request_ids", "weplugins_modify_posts_request_ids_defaults", 10, 2 );
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Example 1: Modifying Post IDs Request
This example demonstrates how to modify the post IDs request based on a custom condition.
function weplugins_modify_posts_request_ids_example1($request, $query) { if ($query->is_home()) { $request = str_replace('SELECT', 'SELECT DISTINCT', $request); } return $request; } add_filter("posts_request_ids", "weplugins_modify_posts_request_ids_example1", 10, 2);
Example 2: Adding Custom SQL Conditions
In this example, we add custom SQL conditions to the post IDs request.
function weplugins_modify_posts_request_ids_example2($request, $query) { if ($query->is_category()) { $request .= " AND wp_posts.post_type = 'custom_type'"; } return $request; } add_filter("posts_request_ids", "weplugins_modify_posts_request_ids_example2", 10, 2);
Example 3: Using Custom Parameters
This example shows how to use custom parameters to modify the post IDs request.
function weplugins_modify_posts_request_ids_example3($request, $query) { if ($query->query_vars['custom_param']) { $request .= " AND wp_posts.meta_key = 'custom_value'"; } return $request; } add_filter("posts_request_ids", "weplugins_modify_posts_request_ids_example3", 10, 2);
Contact Us
If you need any customization or have any queries, feel free to Contact Us.
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.