Skip to content

Instantly share code, notes, and snippets.

@5ally
Created July 23, 2019 05:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5ally/8bf81e6b9e2fad05440a16b2d5137615 to your computer and use it in GitHub Desktop.
Save 5ally/8bf81e6b9e2fad05440a16b2d5137615 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'ajax_query_attachments_args', 'filterMediaLibrary' );
function filterMediaLibrary( $query = array() ) {
if ( empty( $_POST['post_id'] ) ||
( ! $post = get_post( absint( $_POST['post_id'] ) ) ) ) {
return $query;
}
$parent_ids = get_post_ancestors( $post ); // add all ancestors
$parent_ids[] = $post->ID; // add the post itself
$parent_ids = array_merge( $parent_ids, get_children( [ // add all children
'post_parent' => $post->ID,
'fields' => 'ids',
'post_type' => $post->post_type, // or set it to "any".
] ) );
$query['post_parent__in'] = $parent_ids;
return $query;
}
@samjco
Copy link

samjco commented Jul 23, 2019

What is the ids variable?
'fields' => 'ids',

@5ally
Copy link
Author

5ally commented Jul 23, 2019

See WP_Query::parse_query(). But it means to return the post IDs instead of all fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment