Home › Forums › Add-ons › Gallery Field › Different restrictions on different gallery fields › Reply To: Different restrictions on different gallery fields
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.