Support

Account

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

  • There is a document here about adding custom location rules: http://www.advancedcustomfields.com/resources/tutorials/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;
    }