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

What is this, and where do I put this custom code?

add_filter( 'asp_query_args', 'asp_include_only_post_ids', 10, 2 );
function asp_include_only_post_ids( $args, $id ) {
  /**
   * Enter the post IDs here. The results will be
   * limited to these posts/CPT only.   
   */     
  $ids = '1, 2, 3, 4, 5';
  $search_ids = 'all';    // Commma separated list of search IDs, if needed
  
  // -- !! Do not change anything below this line !! --
  $search_ids = wpd_comma_separated_to_array($search_ids);
  if ( in_array('all', $search_ids) || in_array($id, $search_ids) ) {
    $ids = wpd_comma_separated_to_array($ids);
    if ( is_array($args['post_in']) ) {
      $args['post_in'] = array_unique(
        array_merge($args['post_in'], $ids)
      );
    } else {
      $args['post_in'] = $ids;
    }
  }
  
  return $args;
}
  • $ids - comma separated list of Post, Page or any custom post type IDs to restrict the results to

Last updated