diff --git a/wp-media-categories/includes/ajax.php b/wp-media-categories/includes/ajax.php index 93fa8e0..9a19ef8 100644 --- a/wp-media-categories/includes/ajax.php +++ b/wp-media-categories/includes/ajax.php @@ -134,3 +134,40 @@ function wp_media_categories_ajax_update_attachment_taxonomies() { wp_send_json_success( $attachment ); } + +function wp_media_categories_ajax_filter_query( $query ) { + + // Get names of media taxonomies + $taxonomies = get_object_taxonomies( 'attachment', 'names' ); + + $query[ 'tax_query' ] = array( 'relation' => 'AND' ); + + foreach ( $taxonomies as $taxonomy ) { + if ( isset( $query[ $taxonomy ] ) ) { + + // Filter a specific category + if ( is_numeric( $query[ $taxonomy ] ) ) { + array_push( $query[ 'tax_query' ], array( + 'taxonomy' => $taxonomy, + 'field' => 'id', + 'terms' => $query[ $taxonomy ] + ) ); + } + + // Filter No category + if ( $query[ $taxonomy ] == 'no_category' ) { + $all_terms_ids = wp_media_categories_get_terms_values( 'ids' ); + array_push( $query[ 'tax_query' ], array( + 'taxonomy' => $taxonomy, + 'field' => 'id', + 'terms' => $all_terms_ids, + 'operator' => 'NOT IN', + ) ); + } + } + + unset( $query[ $taxonomy ] ); + } + + return $query; +} diff --git a/wp-media-categories/includes/hooks.php b/wp-media-categories/includes/hooks.php index 0c86e29..3ed347c 100644 --- a/wp-media-categories/includes/hooks.php +++ b/wp-media-categories/includes/hooks.php @@ -27,7 +27,10 @@ add_filter( 'attachment_fields_to_edit', 'wp_media_attachment_fields', 10, 2 ); // filter the attachments from user dropdown -add_action( 'wp_ajax_query-attachments', 'wp_media_categories_ajax_query_attachments', 0 ); +//add_action( 'wp_ajax_query-attachments', 'wp_media_categories_ajax_query_attachments', 0 ); + +// filter the attachments from user dropdown by only filter query +add_filter( 'ajax_query_attachments_args', 'wp_media_categories_ajax_filter_query' ); // update the categories add_action( 'wp_ajax_save-attachment-compat', 'wp_media_categories_ajax_update_attachment_taxonomies', 0 );