Home › Forums › Bug Reports › add_filter('acf/fields/taxonomy/query') bug
Hi,
The following code doesn’t work when placed in a plugin file without an action or in an action.
function my_taxonomy_query( $args, $field, $post_id ) {
// modify args
$args['orderby'] = 'count';
$args['order'] = 'ASC';
// return
return $args;
}
add_filter('acf/fields/taxonomy/query', 'my_taxonomy_query');
I’m not sure I understand the problem. The only way that this code may not work is if the place where you’re adding the function is not run for some reason. I’d need to see more information about how it’s being included in the plugin file.
its used like this:
add_action('init', 'UnitedDJ_plugin', 10);
function UnitedDJ_plugin(){
if( !is_admin() )
new UnitedDJ();
}
class UnitedDJ {
public function __construct(){
// Add on acf
if( class_exists('acf_field') ){
include('classes/acf.class.php');
}
}
class UnitedDJ_acf extends UnitedDJ {
public function __construct(){
// general actions
add_filter('acf/fields/taxonomy/query', array( $this, 'kies_je_categorieen_args' ) );
}
}
From looking at the code you provided, your class is only instantiated when you’re not in the admin. When in the admin the class UnitedDJ
is never used. Unless your ACF field is on a front end form then this would not work.
Your filter is in the class UnitedDJ_acf extends UnitedDJ
and does not appear to ever be instantiated so it is never run and the filter is never added.
I just thought of something that may be part of the issue.
The filter runs through ajax and I think, but I don’t recall completely, that ajax is considered and admin function, so your checks for is_admin() return true during an ajax call.
Try changing your if statements to
if( !is_admin() && (!defined('DOING_AJAX') || DOING_AJAX) )
new UnitedDJ();
I have that if statement bass ackwards. You want to instantiate your object if it is an ajax call.
if( !is_admin() || (defined('DOING_AJAX') && DOING_AJAX) )
new UnitedDJ();
I remove the is_admin() tag from the if & still got no reaction.
+ as test I put this in the functions.php file:
function my_taxonomy_query( $args, $field, $post_id ) {
// modify args
$args['orderby'] = 'count';
$args['order'] = 'ASC';
echo '<pre>';
print_r( $args );
echo '</pre>';
exit;
// return
return $args;
}
add_filter('acf/fields/taxonomy/query', 'my_taxonomy_query');
Is it possible the ajax call is never done because its a frontend form?
It should be running, the acf ajax function works for whether or not your logged in.
There is one of two things not going on.
1) Your filter is not being added
2) There is a PHP or JS error that happens that causes it break
For the first, have you tried adding echo 'here'; die;
just before the filter is added to make sure that line in your code is called.
For the second, since it’s an ajax function you won’t be able to tell unless you turn WP error reporting on and log the errors to a file. The code you added `echo ‘<pre>’;
print_r( $args );
echo ‘</pre>’;
` will just cause a php error that causes the ajax to break.
See debugging. https://codex.wordpress.org/Debugging_in_WordPress
add define('WP_DEBUG', true); define('WP_DEBUG_LOG', true);
and then look in the file to see if any php error happen. For JS errors, you’ll need to look at your console and see if any JS errors are reported.
Hi John,
For the display option I used multi select -> checkboxes
Do the checkboxes use a ajax function for display?
I see in the code that the ajax function is only called when the select option is active?
if( !$select.exists() ) { is not false -> line 7258 on acf-input.js ?
PS: There are no errors in the console & no res
Your question got me to digging through the ACF code and testing.
Honestly, I can find this line in the acf code
add_action('wp_ajax_acf/fields/taxonomy/query', array($this, 'ajax_query'));
but I don’t see that this line is ever run. This makes me believe that something has changed since that documentation was written, that there is a bug, or that I’m missing something important.
The filters that are applied are when showing checkboxes are these lines in /fields/taxonomy.php line 691
// filter for 3rd party customization
$args = apply_filters('acf/fields/taxonomy/wp_list_categories', $args, $field);
$args = apply_filters('acf/fields/taxonomy/wp_list_categories/name=' . $field['_name'], $args, $field);
$args = apply_filters('acf/fields/taxonomy/wp_list_categories/key=' . $field['key'], $args, $field);
So I tried this and it worked.
function my_taxonomy_query( $args, $field) {
// modify args
//echo 'here'; die;
$args['orderby'] = 'count';
$args['order'] = 'ASC';
// return
return $args;
}
add_filter('acf/fields/taxonomy/wp_list_categories', 'my_taxonomy_query', 10, 2);
I’m going to mark this thread for the developer’s attention and maybe he’ll comment and let us know what’s going on.
Just want to go on record and point out that I experienced the same issue. All three filters:
acf/fields/taxonomy/query
acf/fields/taxonomy/query/name=
acf/fields/taxonomy/query/key=
fail to actually filter the taxonomy field query. This was tested in a functions.php file with plenty of other valid, working ACF customizations.
Hi guys
The taxonomy field contains 2 filters for modifying the query.
If you are using the taxonomy field as a select element, please use the filter:
acf/fields/taxonomy/query
If you are using it is a checkbox / radio element, please use the filter:
acf/fields/taxonomy/wp_list_categories
Hi,
I currently need to order select/radio by ID (because there’s unfortunately no custom order in WP).
This :
function my_taxonomy_list( $args, $field ) {
if( $field['name'] == 'acf[field_5478963c3fe12]' )
{
$args['orderby'] = 'count';
$args['order'] = 'DESC';
return $args;
}
else return $args;
}
add_filter('acf/fields/taxonomy/wp_list_categories', 'my_taxonomy_list', 10, 2);
used to work but not anymore (I don’t know since when).
acf/fields/taxonomy/wp_list_categories/name= doesn’t work at all (filter never hits).
The topic ‘add_filter('acf/fields/taxonomy/query') bug’ is closed to new replies.
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.