Support

Account

Home Forums Feature Requests More specific location rules: nth-level page.

Solved

More specific location rules: nth-level page.

  • At the moment we can check if the pages has a parent or if the page is a child of another page.

    I need more specific rules. I’d like to show a field group only if the page has a specific amount of ancestors (for example if the page is 3rd level page in the pages hierarchy).

    It would be nice if we could specify the following rules:
    – Show the field group if the page has at least X ancestors
    – Show the field group if the page has at most X ancestors
    – Show the field group if the page is X-level page

    It should be quite simple to implement. WordPress has a buil-in function to get post depth: get_post_ancestors.

  • Thanks. This is exactly what I’m looking for.
    But it would be good if such a feature was available out of the box.

    // Edit;
    I found a little bug in the example you’ve provided. There’s a typo: isset($option['post_id'] instead of isset($options['post_id']

    // Edit 2;
    I’ve found another bug. $page_level doesn’t return a correct value.
    The logic is overcomplicated. It could be simplified:
    $page_level = count(get_post_ancestors($options['post_id'])) + 1; works fine.

    Here’s a fixed version: https://github.com/wujekbogdan/acf-filters-and-functions/blob/patch-1/page-nth-level-location-rule.php

    One more comment:
    There’s one disadvantage of this solution. acf/location/rule_operators filter doesn’t allow to set operators for a specific rule. It’s a general filter that applies for all the rules. But as far I know there’s no other option at the moment.

  • I fixed the first bug thanks, I added that later when I realized by making that change it could be used for any hierarchical post type and needed to make sure the post_id was set to get the post type.

    But, for you’re second one, if you change that then you may have trouble and there is a reason that I do get ancestors on the parent id rather than the post id. When you change the parent of a page this does not become effective until after the post is saved. The AJAX call includes the new parent. Get ancestors on the post ID in the AJAX call can still return the same list of ancestors even if the depth has not changed. And while I could combine that into a single line it is more clear what is being done with it on two lines. Code clarity is more important to me than code brevity.

    Yes, it might be nice to have this is the core of ACF, I can’t do that. I also know that the developer like to keep ACF to what will be used by the majority, among other things. While this could be useful for some I don’t know if it would be useful to most people. That’s why he’s made sure there are plenty of hooks so that we can make modifications.

  • Thanks a lot for your detailed explanation.

  • I’ve marked the thread for the developer to take a look at, he may add it.

    I also agree that it would be nice to have custom operators for location rules so that the ones you add do not appear on all location rules.

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

The topic ‘More specific location rules: nth-level page.’ is closed to new replies.