Limit results to specific post IDs only

Solution #1 - Via back-end option

There is also a back-end option that can be used instead of this code.

Solution #2 - Via custom code

1
add_filter( 'asp_query_args', 'asp_include_only_post_ids', 10, 2 );
2
function asp_include_only_post_ids( $args, $id ) {
3
/**
4
* Enter the post IDs here. The results will be
5
* limited to these posts/CPT only.
6
*/
7
$ids = '1, 2, 3, 4, 5';
8
$search_ids = 'all'; // Commma separated list of search IDs, if needed
9
10
// -- !! Do not change anything below this line !! --
11
$search_ids = wpd_comma_separated_to_array($search_ids);
12
if ( in_array('all', $search_ids) || in_array($id, $search_ids) ) {
13
$ids = wpd_comma_separated_to_array($ids);
14
if ( is_array($args['post_in']) ) {
15
$args['post_in'] = array_unique(
16
array_merge($args['post_in'], $ids)
17
);
18
} else {
19
$args['post_in'] = $ids;
20
}
21
}
22
23
return $args;
24
}
Copied!
    $ids - comma separated list of Post, Page or any custom post type IDs to restrict the results to
Last modified 1yr ago