-
-
Save 5ally/8bf81e6b9e2fad05440a16b2d5137615 to your computer and use it in GitHub Desktop.
See https://wordpress.stackexchange.com/q/343355 for details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the ids variable?
'fields' => 'ids',