Support

Account

Home Forums Backend Issues (wp-admin) Show specific category in Gallery field

Solving

Show specific category in Gallery field

    • tobey_g

    • October 4, 2019 at 10:01 pm

    Is it possible somehow to limit a Gallery field to only show attachments from a certain attachment category? And at the same time assign newly added attachments in that gallery to that same category? I was thinking that there might be some way through filters?

  • WP does not natively support taxonomies for media. I say this because ACF is not likely to support anything that is not supported by core WP.

    To do this a new field type, maybe, for the gallery (an the image field) would need to be built that supports the method that you’re using to add these taxonomies to media posts.

    You may also be able to modify the gallery field? I don’t know.

    First you would need a new field setting to choose a category. You might be able to do this using a filter on acf/render_field_settings. But the media list is done with an AJAX query and without altering the JS involved I don’t know if there is a way to send the value of your setting in the query. There may be information in $_POST that can be used to figure out what field is being loaded, big question mark.

    After that??? I don’t know. I’ve followed, or tried to follow the code, but quite honestly, I don’t see how it works. I do know that it ties into the built in WP media popup dialog. You may be able to limit it using a WP ajax_query_attachment_args filter. When this filter is run you may be able to set the category, and like I said, there may be values in $_POST that tell you what field the query is being done for and then alter the query based on those values.

    Here are some links to simple examples of this filter that limit media to those uploaded by a specific user

    https://www.wpbeginner.com/plugins/how-to-restrict-media-library-access-to-users-own-uploads-in-wordpress/

    https://wordpress.stackexchange.com/questions/1482/restricting-users-to-view-only-media-library-items-they-have-uploaded

    I would start with this filter and I would figure out what values I have available to work with.

    
    // this is a simple function that will log values sent during an ajax query
    // values are added to your error log file
    add_filter( 'ajax_query_attachments_args', 'log_attachment_query' );
    function log_attachment_query($query) {
      ob_start();
      print_r($query);
      print_r($_POST);
      error_log(ob_get_clean());
      return $query;
    }
    

    This topic has come up in the past here, multiple times, unfortunately, I cannot find it or remember if a solution was ever found.

  • I found a previous reply of mine that has more information

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.