No estoy 100% seguro de resolver el problema, pero ... Tal vez esto te ayude ...
El cargador de medios obtiene archivos adjuntos con un simple WP_Query
, por lo que puedes usar muchos filtros para modificar su contenido.
El único problema es que no puede consultar publicaciones con CPT específico como principal utilizando los argumentos WP_Query
... Por lo tanto, tendremos que usar los filtros posts_where
y posts_join
.
Para estar seguros de que solo cambiaremos la consulta del cargador de medios, usaremos ajax_query_attachments_args
.
Y así es como se ve, cuando se combina:
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');
Al abrir el cuadro de diálogo del cargador de medios al editar la publicación (publicación / página / CPT), solo verá las imágenes adjuntas a este tipo de publicación específica.
Si desea que funcione solo para un tipo de publicación específica (digamos páginas), tendrá que cambiar la condición en la función my_posts_where
de esta manera:
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;
}