Não tenho 100% de certeza se acerto seu problema, mas ... Talvez isso ajude você ...
O remetente de mídia obtém anexos com WP_Query
simples, para que você possa usar vários filtros para modificar seu conteúdo.
O único problema é que você não pode consultar postagens com CPT específico como pai usando argumentos WP_Query
... Assim, teremos que usar os filtros posts_where
e posts_join
.
Para garantir que alteraremos apenas a consulta do remetente de mídia, usaremos ajax_query_attachments_args
.
E é assim que parece, quando combinado:
function my_posts_where($where) {
global $wpdb;
$post_id = false;
if ( isset($_POST['post_id']) ) {
$post_id = $_POST['post_id'];
$post = get_post($post_id);
if ( $post ) {
$where .= $wpdb->prepare(" AND my_post_parent.post_type = %s ", $post->post_type);
}
}
return $where;
}
function my_posts_join($join) {
global $wpdb;
$join .= " LEFT JOIN {$wpdb->posts} as my_post_parent ON ({$wpdb->posts}.post_parent = my_post_parent.ID) ";
return $join;
}
function my_bind_media_uploader_special_filters($query) {
add_filter('posts_where', 'my_posts_where');
add_filter('posts_join', 'my_posts_join');
return $query;
}
add_filter('ajax_query_attachments_args', 'my_bind_media_uploader_special_filters');
Quando você abre a caixa de diálogo de envio de mídia ao editar post (post / page / CPT), verá apenas imagens anexadas a esse tipo de postagem específico.
Se você quiser que ele funcione apenas para um tipo de postagem específico (digamos, páginas), você terá que alterar a condição na função my_posts_where
assim:
function my_posts_where($where) {
global $wpdb;
$post_id = false;
if ( isset($_POST['post_id']) ) {
$post_id = $_POST['post_id'];
$post = get_post($post_id);
if ( $post && 'page' == $post->post_type ) { // you can change 'page' to any other post type
$where .= $wpdb->prepare(" AND my_post_parent.post_type = %s ", $post->post_type);
}
}
return $where;
}