Limiting results to specific posts by parent ID

While there is no option to limit the results pool to posts with specific parents only, it is possible with a filter, very easily.

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

add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
function asp_include_only_parent_ids( $args, $id ) {
  /**
   * Enter the post/cpt prent IDs here. The results will be
   * limited to objects with these parent IDs.   
   */     
  $ids = array(1, 2, 3, 4, 5, 6);
  
  // -- !! Do not change anything below this line !! --
  $args['post_parent'] = $ids;
  
  return $args;
}

..same code, but to apply only for specific search instances:

add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
function asp_include_only_parent_ids( $args, $id ) {
  /**
   * Enter the post/cpt prent IDs here. The results will be
   * limited to objects with these parent IDs.   
   */     
  $ids = array(1, 2, 3, 4, 5, 6);
  /**
   * Search instance IDs you want this code to apply on.
   */
  $search_ids = array(1, 2);      
  
  // --------------------------------------------------
  // --------------------------------------------------
  // -- !! Do not change anything below this line !! --
  // --------------------------------------------------
  if ( in_array($id, $search_ids) )
    $args['post_parent'] = $ids;
  
  return $args;
}

Last updated