Home › Forums › ACF PRO › acf/fields/taxonomy/query doesn't return the same result which get_terms() does
First of all, big thank U for ur amazing plugin which has been saving my life!
This time, I am trying to limit taxonomy field’s selection to only children. (or exclude parents)
using acf/fields/taxonomy/query/name=(fieldname).
Firstly, I can get the right result by the code below.
$args = array(
'hide_empty' => 0,
'fields' => 'ids',
);
$all_ids = get_terms( '(sometaxonomy)', $args );
$args = array(
'hide_empty' => 0,
'parent' => 0,
'fields' => 'ids',
);
$parent_ids = get_terms( '(sometaxonomy)', $args );
$args = array(
'hide_empty' => 0,
'include' => array_diff( $all_ids, $parent_ids ),
);
$children = get_terms( '(sometaxonomy)', $args );
* note: ‘excluding parents’ did not work…
But when I put it in a function which is hooked upto acf/fields/taxonomy/query/name=(fieldname) like below, I get “No matches found” on the screen.
function myfunction( $args, $field, $post_id ) {
$args = array(
'hide_empty' => 0,
'fields' => 'ids',
);
$all_ids = get_terms( 'store_cat', $args );
$args = array(
'hide_empty' => 0,
'parent' => 0,
'fields' => 'ids',
);
$parent_ids = get_terms( 'store_cat', $args );
$args = array(
'hide_empty' => 0,
'include' => array_diff($all_ids, $parent_ids),
);
//var_dump( get_terms( 'store_cat', $args ) );
return $args;
}
add_filter('acf/fields/taxonomy/query/name=storecat_tax', 'myfunction', 10, 3);
then when I un-escape ‘var_dump’ line, I can check the result of get_terms() is correct in a browser.
Does the hook modify the arg internaly?
or
Am I writing wrong code?
I’ll appreciate any sort of help/advice.
Thanks in advance!
regards
Hi @hiroshi
I’m afraid this is not possible because ACF will try to get the parents if the taxonomy is hierarchical so that it can show the terms in a tree layout. To fix it, you need to modify the core file. In “wp-content/plugins/advanced-custom-fields-pro/fields/taxonomy.php”, please search this line of code:
// this will fail if a search has taken place because parents wont exist
if( empty($args['search']) ) {
Kindly change it to this one:
// this will fail if a search has taken place because parents wont exist
if( empty($args['search']) && $parent ) {
After that, you can use this code to show only the child:
function myfunction( $args, $field, $post_id ) {
$parents_args = array(
'hide_empty' => 0,
'parent' => 0,
'fields' => 'ids',
);
$parent_ids = get_terms( 'custom-taxonomy', $parents_args );
$args['exclude'] = $parent_ids;
return $args;
}
add_filter('acf/fields/taxonomy/query/name=taxonomy', 'myfunction', 10, 3);
Also, could you please open a new ticket so this issue can be passed directly to the plugin author? You can open a new ticket here: https://support.advancedcustomfields.com/new-ticket.
Thanks š
Hi James!
Big thank you for your support as usual!
Iām afraid this is not possible because ACF will try to get the parents if the taxonomy is hierarchical so that it can show the terms in a tree layout. To fix it, you need to modify the core file.
I understand.
Instead of modifying the core file,
Do you think making new add-on based on the file is bad idea?
PS
As a temporary solution, giving up query solution & using acf/fields/taxonomy/result/ hook, I added some text such as ‘[not selectable]’ to each selection’s title , and I prevent a click on it by Jquery & css. (pink background row is not clickable in the attachment)
PS2
I don’t know how I should describe my issue as a ticket though…
should it be a bug report? or request?
Hi @hiroshi,
Thanks for the follow up.
I am glad you found a workaround for this issue.
Please send this to our support team as a feature request so that this can be brought to the attention of the plugin author.
You can also share the link to this topic as a reference in your ticket!.
Hi @hiroshi
If you want to create a new add-on (plugin), please check this page: https://codex.wordpress.org/Writing_a_Plugin.
I’m afraid ACF doesn’t have a tutorial to create ACF add-on. But if you want to create a new field type like “child terms” (only shows child terms), for example, you can follow this guide: https://www.advancedcustomfields.com/resources/creating-a-new-field-type/.
I hope this helps š
Hi @james
Using starter kit which you introduced, I could make my own one!
Thank you for your continuous support!
I really appreciate it!
The topic ‘acf/fields/taxonomy/query doesn't return the same result which get_terms() does’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.