Using WordPress ‘posts_request_ids’ PHP filter

The posts_request_ids filter allows you to modify the Post IDs SQL request before it’s sent to the database. This can be useful for customizing query results in various ways.

Usage

add_filter('posts_request_ids', 'my_custom_posts_request_ids', 10, 2);
function my_custom_posts_request_ids($request, $query) {
// your custom code here
return $request;
}

Parameters

  • $request (string): The post ID request that will be sent to the database.
  • $query (WP_Query): The WP_Query instance related to the current request.

More information

See WordPress Developer Resources: posts_request_ids

Examples

Exclude Specific Post IDs

Exclude specific post IDs from the query by modifying the SQL request.

add_filter('posts_request_ids', 'exclude_specific_post_ids', 10, 2);
function exclude_specific_post_ids($request, $query) {
global $wpdb;
$exclude_ids = array(1, 2, 3); // Set the post IDs you want to exclude
$exclude_ids = implode(',', $exclude_ids);
$request = str_replace("FROM {$wpdb->posts}", "FROM {$wpdb->posts} WHERE {$wpdb->posts}.ID NOT IN ({$exclude_ids})", $request);
return $request;
}

Limit Number of Post IDs

Limit the number of post IDs returned by the SQL request.

add_filter('posts_request_ids', 'limit_number_of_post_ids', 10, 2);
function limit_number_of_post_ids($request, $query) {
$request .= " LIMIT 5"; // Limit the number of post IDs to 5
return $request;
}

Order Post IDs by Custom Field

Order post IDs based on a custom field value.

add_filter('posts_request_ids', 'order_post_ids_by_custom_field', 10, 2);
function order_post_ids_by_custom_field($request, $query) {
global $wpdb;
$meta_key = 'custom_field_name'; // Replace with your custom field name
$request = str_replace("FROM {$wpdb->posts}", "FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '{$meta_key}' )", $request);
$request .= " ORDER BY {$wpdb->postmeta}.meta_value ASC"; // Sort by the custom field value (ASC or DESC)
return $request;
}

Filter Post IDs by Custom Taxonomy

Filter post IDs based on a custom taxonomy term.

add_filter('posts_request_ids', 'filter_post_ids_by_custom_taxonomy', 10, 2);
function filter_post_ids_by_custom_taxonomy($request, $query) {
global $wpdb;
$taxonomy = 'custom_taxonomy'; // Replace with your custom taxonomy name
$term_id = 10; // Replace with your desired term ID
$request = str_replace("FROM {$wpdb->posts}", "FROM {$wpdb->posts} INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) INNER JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id AND {$wpdb->term_taxonomy}.taxonomy = '{$taxonomy}' AND {$wpdb->term_taxonomy}.term_id = {$term_id})", $request);
return $request;
}

Exclude Post IDs in a Date Range

Exclude post IDs that fall within a specific date range.

add_filter('posts_request_ids', 'exclude_post_ids_in_date_range', 10, 2);
function exclude_post_ids_in_date_range($request, $query) {
global $wpdb;
$start_date = '2023-01-01'; // Set the start date
$end_date = '2023-01-31'; // Set the end date
$request = str_replace("FROM {$wpdb->posts}", "FROM {$wpdb->posts} WHERE ({$wpdb->posts}.post_date NOT BETWEEN '{$start_date}' AND '{$end_date}')", $request);
return $request;
}