From 81408be24b5e7a8b5a40d1d41be9f5b5abb1afcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Sikorski?= Date: Sat, 31 Jul 2021 11:40:26 +0200 Subject: [PATCH 1/2] Update ajax.php --- wp-media-categories/includes/ajax.php | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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; +} From 4f23c121809ae405af99dd48f05ea99c1a651325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Sikorski?= Date: Sat, 31 Jul 2021 11:45:39 +0200 Subject: [PATCH 2/2] Update hooks.php --- wp-media-categories/includes/hooks.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 );