Home Forums Backend Issues (wp-admin) Assigning Custom Fields To Child/Parents?


Assigning Custom Fields To Child/Parents?

  • Is it possible to assign a custom fields to a custom post type parent ONLY and subsequently a custom post type child ONLY?

    So you have a custom post type parent with a few children, but even though they are the same custom post type, different custom fields appear whether they are parents or children?

  • There is a document here about adding custom location rules:

    I don’t have an example of adding a custom location rule for a post parent/child rule, but I do have an example of adding a taxonomy parent/child rule.

    This is all built into a class for a custom theme I built for a client, hopefully it helps you get an idea what you need to do.

    private function __construct() {
      add_filter('acf/location/rule_types', array($this, 'acf_location_rules_types'));
      add_filter('acf/location/rule_operators', array($this, 'acf_location_rules_operators'));
      add_filter('acf/location/rule_values/taxonomy_depth', array($this, 'acf_location_rules_values_taxonomy_depth'));
      add_filter('acf/location/rule_match/taxonomy_depth', array($this, 'acf_location_rules_match_taxonomy_depth'), 10, 3);
    public function acf_location_rules_types($choices) {
      $choices['Other']['taxonomy_depth'] = 'Taxonomy Depth';
      return $choices;
    public function acf_location_rules_operators($choices) {
      //BY DEFAULT WE HAVE == AND !=
      $choices['<'] = 'is less than';
      $choices['>'] = 'is greater than';
      return $choices;
    public function acf_location_rules_values_taxonomy_depth($choices) {
      for ($i=0; $i<6; $i++) {
       $choices[$i] = $i;
      return $choices;
    public function acf_location_rules_match_taxonomy_depth($match, $rule, $options) {
      $depth = intval($rule['value']);
      if (isset($_GET['taxonomy']) && isset($_GET['tag_ID'])) {
        $term_depth = count(get_ancestors($_GET['tag_ID'], $_GET['taxonomy']));
      if($rule['operator'] == '==') {
        $match = ($depth == $term_depth);
      } elseif($rule['operator'] == '!=') {
        $match = ($depth == $term_depth);
      } elseif($rule['operator'] == '<') {
        $match = ($term_depth < $depth);
      } elseif($rule['operator'] == '>') {
        $match = ($term_depth > $depth);
      return $match;
  • Thank you Hube2, i’ll try and have a look. Is there anywhere what shows what can be included within there.

    Thats the the bit i don’t understand, i as i need to select the parent or the child from a specific custom post type, not sure if there is enough boxes..?

  • I imagine that you’ll need to do several things. The first three functions that set up the rules when creating a field group in ACF should be pretty straight forward. Actually I don’t think you need to use acf_location_rules_operator() because I would use the standard == != and use “Top Level Post” or “Child Post” for the choices.

    Your custom rules also do not need to make the post_type selection. When creating your group you’d just use two rules

    • Post Type == your custom post type
    • Post Level == top level or child

    That leaves the function for testing the parent/child rule. Take a look at this for information on testing to see if a page is a child page:

  • You know what?

    I was just creating a new field group and realized you don’t need to build any of this. There is already a “Page Type” selection with “Top Level/Parent/Child” choices.

    Don’t ask me what I was thinking, but I assumed that since you were asking about this that it did not already exist.

  • Thanks, i didn’t realise i could assign to a custom post then subsequently a page parent, i just assumed that was only for pages themselves.


  • Just a “thank you” for this — I had the same question and didn’t realize those would apply to hierarchical CPTs as well.

  • Hi @hube2 I use this two conditions :

    “block” + “is equal to” + “name of my block”
    “Page Type” + “is not equal to” + “Parent Page (has children)”

    but my block also appears in parent pages with children. Do you know this problem ? Or have you a solution ? Thank you 🙂

  • if

    “block” + “is equal to” + “name of my block”

    refers to a Gutenberg block then I can’t help you.

  • Thanks for your response @hube2

    Just to be clear :
    “block” + “is equal to” + “name of my block”
    It is not a custom code, it is a location rules provided by ACF.

  • Yes, I know, but you directed you’re question to me directly so I answered. I don’t use, and have no plan to ever use, the new block editor for the themes that I build. So when it comes to any question about ACF that involves the new editor I mostly haven’t got a clue.

    You should probably start a new topic and hopefully an ACF user that does use the new editor might be able to help you out.

  • I understand. Thanks for taking time to respond to me. And thank you very much for this quick response :))

  • Hi,

    This is awesome !
    Is it possible to do the same with custom taxonomy ?

    because if I add page type with or without child / parent it doesn’t work as it is not a page but a taxonomy.

  • @himself no, this is not possible at the moment in native ACF. You would have to build your own location rules. There are many examples of code to do this on the web.

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

The topic ‘Assigning Custom Fields To Child/Parents?’ is closed to new replies.