Filter Media Library by author or post_parent

The only way to do it is using a query filter I think

function wpse156319_posts_where( $where, $query ) {
    global $wpdb;
    $where .= $wpdb->prepare( ' AND (' . $wpdb->posts . '.post_parent = %s OR ' . $wpdb->posts . '.post_author = %s)', $_POST['id'], get_current_user_id() );
    return $where;
}

which you just add in the ajax_query_attachments_args instead of doing anything else

function filterMediaLibrary($query = array()) {
    add_filter( 'posts_where', 'wpse156319_posts_where', 10, 2 );
    return $query;
}

Seeing as it’s a one-off ajax query it shouldn’t need removing.

Leave a Comment