• Resolved xanathon

    (@xanathon)


    Hello,

    I want to create a mla gallery page via shortcode showing only media items that have no category attached.

    Is this possible, and if yes, how? I tried to search the posts here but did not find any hints to this special problem.

    Edit: Additional related question: Or is it possible to assign a standard category to new items?

    Thanks in advance,
    Xanathon

    • This topic was modified 7 years, 5 months ago by xanathon.
    • This topic was modified 7 years, 5 months ago by xanathon.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author David Lingren

    (@dglingren)

    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.

    Thread Starter xanathon

    (@xanathon)

    Hello David,

    Thank you for your extremely helpful insights!

    I will test this as soon as I find time.

    Regarding the autoassignment of categories:

    I have a script that generates images after some user-interactions. The images are saved to the media library, if a standard category could be assigned, that would help me to show a gallery that consists only of these generated images. I am aware of the bulk edit possibilities, but those do not help in my use case.

    So if I assign categories to all manually uploaded images, I can show the script generated ones if I list only those without any category, thus my question.

    Thanks again for your detailed reply.

    Regards,
    Xanathon

    Plugin Author David Lingren

    (@dglingren)

    Thanks for the kind words and the promise to test the update.

    You wrote “I have a script that generates images …” If you are adding the Media Library items in a PHP script you can use any of the WordPress functions, such as wp_set_object_terms(), to add taxonomy terms to the generated items. You could create a term unique to the generated items and filter an [mla_gallery] by that term. Would that work for you?

    Thread Starter xanathon

    (@xanathon)

    It’s not my script, I bought it on Envato, so I would have to sift through the code to look up the part where the image is saved … But I’ll try your development version and the shortcode shown above first. ??

    Thanks again.

    Plugin Author David Lingren

    (@dglingren)

    I have released MLA version 2.61, which contains the new no.terms.assigned parameter value for filtering items that have no term assignments in a given taxonomy.

    I am marking this topic resolved, but please update it if you have any problems or further questions regarding the new feature or the suggestions in my earlier posts. Thanks for inspiring an MLA enhancement and for your interest in the plugin.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘MLA Gallery with items without category’ is closed to new replies.