The found_posts_query WordPress PHP filter allows you to modify the query used for retrieving the found posts.
Usage
add_filter('found_posts_query', 'your_custom_function', 10, 2); function your_custom_function($found_posts_query, $query) { // your custom code here return $found_posts_query; }
Parameters
$found_posts_query
(string) – The query to run to find the found posts.$query
(WP_Query) – The WP_Query instance (passed by reference).
More information
See WordPress Developer Resources: found_posts_query
Examples
Modify the found posts query to include specific post types
In this example, we’ll modify the found posts query to include only ‘post’ and ‘page’ post types.
function filter_found_posts_query($found_posts_query, $query) { global $wpdb; $found_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ('post', 'page') AND post_status = 'publish'"; return $found_posts_query; } add_filter('found_posts_query', 'filter_found_posts_query', 10, 2);
Exclude posts from a specific category
In this example, we’ll exclude posts from a specific category (category ID 5) from the found posts query.
function exclude_category_from_found_posts_query($found_posts_query, $query) { global $wpdb; $found_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE ID NOT IN (SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = 5) AND post_status = 'publish'"; return $found_posts_query; } add_filter('found_posts_query', 'exclude_category_from_found_posts_query', 10, 2);
Filter the found posts query based on a custom field value
In this example, we’ll modify the found posts query to show only posts with a specific custom field value.
function filter_by_custom_field($found_posts_query, $query) { global $wpdb; $found_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE {$wpdb->postmeta}.meta_key = 'custom_field_key' AND {$wpdb->postmeta}.meta_value = 'custom_value' AND post_status = 'publish'"; return $found_posts_query; } add_filter('found_posts_query', 'filter_by_custom_field', 10, 2);
Change the found posts query to count only posts from the last 30 days
In this example, we’ll modify the found posts query to count only posts published within the last 30 days.
function filter_last_30_days_posts($found_posts_query, $query) { global $wpdb; $found_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND post_status = 'publish'"; return $found_posts_query; } add_filter('found_posts_query', 'filter_last_30_days_posts', 10, 2);
Modify the found posts query based on the current user role
In this example, we’ll modify the found posts query to show different posts based on the current user’s role.
function filter_by_user_role($found_posts_query,$found_posts_query, $query) { global $wpdb; if (current_user_can('editor')) { // Show only 'post' post type for editors $found_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'"; } elseif (current_user_can('author')) { // Show only 'page' post type for authors $found_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'page' AND post_status = 'publish'"; } return $found_posts_query; } add_filter('found_posts_query', 'filter_by_user_role', 10, 2);