Thanks for an interesting question. Finding items with no assigned terms in a taxonomy is not directly supported in the WP_Query class used by MLA for the Media/Assistant submenu table and the [mla_gallery]
shortcode. The closest approximation in WP_Query is something like “select items with terms NOT IN any of the existing terms”, e.g., something like:
$args = array(
array(
'taxonomy' => 'attachment_category',
'field' => 'id',
'terms' => array( 1,2,3 ),
'operator' => 'NOT IN'
)
);
The problem, of course, is maintaining the 'terms' => array( 1,2,3 ),
element as new terms are added. MLA solves this for the Media/Assistant submenu table with the following code:
$term_list = get_terms( 'attachment_category', array(
'fields' => 'ids',
'hide_empty' => false
) );
$args = array(
array(
'taxonomy' => 'attachment_category',
'field' => 'id',
'terms' => $term_list,
'operator' => 'NOT IN'
)
);
That works fine, but could get awkward if your taxonomy has hundreds or thousands of terms. WP_Query generates something like the following:
SELECT SQL_CALC_FOUND_ROWS mladev_posts.ID
FROM mladev_posts
WHERE 1=1
AND ( mladev_posts.ID NOT IN (
SELECT object_id
FROM mladev_term_relationships
WHERE term_taxonomy_id IN (11,12,26,35,36,167,184,185,186,187,188,189,235,236,237,238,240,241,250,251,290,291,295,312,323,324,325,326,
... many more term IDs ...
2253,2254,2256,2257,2258) ) )
AND mladev_posts.post_type = 'attachment'
AND ((mladev_posts.post_status = 'inherit'))
GROUP BY mladev_posts.ID
ORDER BY mladev_posts.ID DESC
LIMIT 0, 20
I did some SQL hacking and came up with an alternative that produces the same results:
SELECT * FROM <code>mladev_posts</code> as p
WHERE p.post_type = 'attachment' AND p.post_status = 'inherit'
AND p.ID NOT IN (
SELECT DISTINCT tr.object_id FROM <code>mladev_term_taxonomy</code> AS tt
JOIN <code>mladev_term_relationships</code> AS tr
WHERE ( tt.taxonomy = 'attachment_category' ) AND tt.term_taxonomy_id = tr.term_taxonomy_id
)
However, I haven’t found a way to integrate the alternative query with other WP_Query parameters.
I did find a way to extend [mla_gallery]
to produce the first, WP_Query, alternative. A new parameter value, no.terms.assigned
, signals [mla_gallery]
to dynamically get all the current term ID values and creates the “NOT IN” query for them. You can use the new value in either of two ways. First, for the “simple taxonomy query” you can code something like:
[mla_gallery attachment_category="no.terms.assigned"]
Second, for the more powerful tax_query
you can use:
[mla_gallery]
tax_query="array(
array(
'taxonomy'=>'attachment_category',
'field'=>'id',
'terms'=> 'no.terms.assigned',
'operator' => 'NOT IN',
),
)"
[/mla_gallery]
In addition to 'terms' => 'no.terms.assigned'
you must code 'field' => 'id'
and 'operator' => 'NOT IN'
to get the proper results. The example uses use the “enclosing shortcode” format to avoid problems WordPress has in parsing parameters with special characters such as =>
.
I have uploaded a new MLA Development Version that contains the enhancements. You can find detailed instructions for getting and installing the Development Version in this earlier topic:
Create a feed out of the media library
It would be great if you can install the Development Version and let me know if it works for you.
You asked “Is it possible to assign a standard category to new items?” There’s no way to specify this as a permanent default in the current MLA version but you can use the Bulk Edit area on the Media/Assistant admin screen or the Media/Add New (Upload New Media) screen. You can specify the category you want on either screen and apply it to many items at once.
Thanks for your interest in the plugin and for inspiring a new feature.