• Hi there, for a custom post type I have defined a field (type: taxonomy terms) where the backend user can pick from certain taxonomies (here: some custom taxonomies and the post categories). The pick values are generated here: acf-extended/includes/fields/field-taxonomy-terms.php@convert_terms_to_choices(). There is a function call acf_get_grouped_terms() in line 1409. This function is defined in advanced-custom-fields-pro/includes/api/api-term.php. In this function, all categories are received by a parameter. What I don’t understand is, why are these categories sliced (line 253) based on an offset (line 236). 14 categories get in, only the last three get out. I do not see the reason for this behaviour, could you please explain what I am doing wrong or is this a bug?

    Best regards,
    Henning

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    The acf_get_grouped_terms() function is a native ACF function which allow to query multiple taxonomies at the same time, and regroup terms within their parent taxonomy. It’s pretty standard in the ACF code base, and it’s used at many different place.

    The ACF developer left some comment that explain why the array_slice is used in the code, but you probably already seen it:

    // compare aray lengths
    // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
    // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)

    You can quickly check what the function returns using something like:

    acf_get_grouped_terms(array(
    'taxonomy' => 'category'
    ));

    Generally speaking, the issue you’re describing (terms that don’t seem to load correctly) is related to Multilingual setups. In fact, if you have setup a multilingual installation, terms that aren’t translated in the “currently selected language” will not be displayed. This is how ACF handle this use case by default.

    Is this your case?

    Thanks!

    Regards.

    Thread Starter henning22

    (@henning22)

    Hi Konrad,

    thanks for your reply. I’ve checked the return value like you suggested and I get all (in my case) 14 categories. We have no multilanguage setup, so this couldn’t be the cause.

    If I run the check exactly like it’s called in convert_terms_to_choices from wp shell it returns the correct category values (all 14) besides the other allowed taxonomies. If it’s called from the backend, the categories are sliced.

    I don’t know if this is relevant, but there are multiple taxonomies defined for this taxonomy_terms field. But even if I remove the other allowed taxonomies, the result for the categories stays the same.

    Best regards,
    Henning

    Edit: I’ve done another code review and found this one:

    // compare aray lengths
    // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
    // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)
    if ( count( $ordered_terms ) == count( $all_terms ) ) {
    $this_terms = array_slice( $ordered_terms, $offset, $length );
    }

    The comment says “…is smaller than…” but the if condition says “equals (==)”. Could this be the cause for the malfunction. In my opinion the condition has to be “<“. If I am right, this would be a bug in “Advanced Custom fields Pro”, right?

    • This reply was modified 2 weeks, 1 day ago by henning22.
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    In order to isolate the issue, I would recommend to run your test directly with acf_get_grouped_terms(). You can run the following simple function:

    acf_get_grouped_terms(array(
    'taxonomy' => 'category' // or use your specific taxonomy
    ));

    If you get different results from wp shell and from within the WP Admin, then it might mean you got some custom code either in a third party plugin or in your theme, which would interfere with the returning terms.

    To find the root of the problem, I would recommend clone your website on a development / staging environment using Duplicator, or with your hosting backup solution. Then try to disable all your plugins one-by-one (at the exception of ACF Pro) until you find which one cause the issue.

    If the problem persists, try switch to the native WP Twenty Twenty Two theme, to make sure you don’t have any custom code in your theme which would break the feature.

    I don’t think the issue come from the ACF code itself. This function and its logic has been around for few years now. But if the issue can be reproduced on a clean WP install with ACF Pro only, then yeah it might be a bug that should be reported to ACF.

    Let me know if you found something.

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.