Support

Account

Home Forums Add-ons Gallery Field Different restrictions on different gallery fields

Solved

Different restrictions on different gallery fields

  • Hi,

    On a gallery field, i know it’s possible to restrict media items using the ajax_query_attachments_args filter. It’s great for adding a user restriction for example, allowing users to only chose among media they’ve uploaded themselves.

    The thing is i’d like to be able to add different restrictions on different gallery fields for the same post type, based on relationships between post types for example.

    In my case, i got a Portfolio CPT that have a relationship field pointing to a Product CPT (“I used products A, B and C on Project X”). I have a particular gallery field on each Product that i want to show only images attached to related folios …

    Up to now, it only seams possible to restrict media items for the whole product edition page, using something like this :

    add_filter( 'ajax_query_attachments_args', 'filter_product_attachments', 10, 1 );
    function filter_product_attachments( $query = array() ) {
        if (is_admin() && get_post_type() == 'product'){
          // Filter media query here
        }
        return $query;
    }

    Any idea on how to do this only for a particular gallery field ?

  • Using the filter hook you posted I did some investigation and I can say, yes, you can modify the query based on the acf field.

    I want to explain how I figured this out for anyone that happens on this and is looking to figure other things out.

    I have this function that I add to my functions.php file whenever I want to figure out what’s going on in a ajax call because you can’t use the standard php debugging techniques like echo and print_r(). What it does is it writes stuff to a file on the server so I can see what I want to see without interrupting the AJAX call and without writing miles of code to alter WP code the get an output.

    
    function write_to_file($value) {
      // this function for testing & debuggin only
      // do not leave this function working on your site
      $file = dirname(__FILE__).'/__data.txt';
      $handle = fopen($file, 'a');
      ob_start();
      if (is_array($value) || is_object($value)) {
        print_r($value);
      } elseif (is_bool($value)) {
        var_dump($value);
      } else {
        echo $value;
      }
      echo "\r\n\r\n";
      fwrite($handle, ob_get_clean());
      fclose($handle);
    }
    

    So then what I did was set up a filter for ajax_query_attachments_args that looks like this.

    
    function write_attachment_query_to_file($query=array()) {
      write_to_file($_POST);
      write_to_file($query);
      return $query;
    }
    

    and this is what I got

    
    Array
    (
        [action] => query-attachments
        [post_id] => 838
        [query] => Array
            (
                [post_mime_type] => image
                [orderby] => date
                [order] => DESC
                [posts_per_page] => 40
                [_acfuploader] => field_579b87ce88001
                [paged] => 1
            )
    
    )
    
    Array
    (
        [post_mime_type] => image
        [orderby] => date
        [order] => DESC
        [posts_per_page] => 40
        [paged] => 1
        [post_type] => attachment
        [post_status] => inherit,private
    )
    

    the first array is the $_POST array and $_POST['query']['_acfuploader'] holds the field key for the image field that popped up the media library.

    So, what you need to do is see if this value is set and then use that to determine how to modify the media query.

  • Impressive !
    Thank you very much for the help.

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

The topic ‘Different restrictions on different gallery fields’ is closed to new replies.