Excluding posts or CPT by parent ID(s)

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

add_filter( 'asp_query_args', 'asp_exclude_by_parent_ids', 10, 2 );
function asp_exclude_by_parent_ids( $args, $id ) {
  /**
   * Enter the post/cpt parent IDs here, that you want to exclude
   */     
  $ids = array(1, 2, 3, 4, 5, 6);
  
  // -- !! Do not change anything below this line !! --
  $args['post_parent_exclude'] = $ids;
  
  return $args;
}

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

add_filter( 'asp_query_args', 'asp_exclude_by_parent_ids', 10, 2 );
  
function asp_exclude_by_parent_ids( $args, $id ) {
  /**
   * Enter the post/cpt parent IDs here, that you want to exclude
   */    
  $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_exclude'] = $ids;
  
  return $args;
}

Last updated

Copyright Ernest Marcinko