Support

Account

Home Forums ACF PRO Rules for sub Category

Solving

Rules for sub Category

    • pagol

    • December 24, 2017 at 6:09 am

    I want to make rules for main category and it will effect all sub category also.
    ex.
    Gallery
    Sub gallery 1
    Sub gallery 2
    Sub gallery 3

    so if put rules for gallery then it will effect on all sub gallery also. how can do it ?

  • This is a copy of my example here https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-post-category-ancestor-location-rule.php with 1 line difference

    This difference is marked, look for

    
    // this line added to include this term
    // **********************************************************************************
    $ancestors[] = $term_to_check->term_id;
    // **********************************************************************************
    

    You may want to change other things, like the value of the location rule, maybe the function names.

    I have not tested this, but if my example is still working then this change should make it include any category and any children of that category

    
    <?php 
      
    // category ancestor location rule
    add_filter('acf/location/rule_types', 'acf_location_types_category_ancestor');
    function acf_location_types_category_ancestor($choices) {
      if (!isset($choices['Post']['post_category_ancestor'])) {
        $choices['Post']['post_category_ancestor'] = 'Post Category Ancestor';
      }
      return $choices;
    }
    
    add_filter('acf/location/rule_values/post_category_ancestor', 'acf_location_rule_values_category_ancestor');
    function acf_location_rule_values_category_ancestor($choices) {
      // copied from acf rules values for post_category
      $terms = acf_get_taxonomy_terms('category');
      if (!empty($terms)) {
        $choices = array_pop($terms);
      }
      return $choices;
    }
    
    add_filter('acf/location/rule_match/post_category_ancestor', 'acf_location_rule_match_category_ancestor', 10, 3);
    function acf_location_rule_match_category_ancestor($match, $rule, $options) {
      // most of this copied directly from acf post category rule
      $terms = 0;
      if (array_key_exists('post_taxonomy', $options)){
        $terms = $options['post_taxonomy'];
      }
      $data = acf_decode_taxonomy_term($rule['value']);
      $term = get_term_by('slug', $data['term'], $data['taxonomy']);
      if (!$term && is_numeric($data['term'])) {
        $term = get_term_by('id', $data['term'], $data['taxonomy']);
      }
      // this is where it's different than ACf
      // get terms so we can look at the parents
      if (is_array($terms)) {
        foreach ($terms as $index => $term_id) {
          $terms[$index] = get_term_by('id', intval($term_id), $term->taxonomy);
        }
      }
      if (!is_array($terms) && $options['post_id']) {
        $terms = wp_get_post_terms(intval($options['post_id']), $term->taxonomy);
      }
      if (!is_array($terms)) {
        $terms = array($terms);
      }
      $terms = array_filter($terms);
      $match = false;
      // collect a list of ancestors
      $ancestors = array();
      if (count($terms)) {
        foreach ($terms as $term_to_check) {
          // this line added to include this term
          // **********************************************************************************
          $ancestors[] = $term_to_check->term_id;
          // **********************************************************************************
          $ancestors = array_merge(get_ancestors($term_to_check->term_id, $term->taxonomy));
        } // end foreach terms
      } // end if
      // see if the rule matches any term ancetor
      if ($term && in_array($term->term_id, $ancestors)) {
        $match = true;
      }
    
      if ($rule['operator'] == '!=') {
        // reverse the result
        $match = !$match;
      }
      return $match;
    }
      
    ?>
    
    • pagol

    • December 30, 2017 at 7:12 am

    should i need to open acf-post-category-ancestor-location-rule.php file and replace your code ?

  • The above is a custom location rule based on the documentation https://www.advancedcustomfields.com/resources/custom-location-rules/ and is not part of ACF.

    • pagol

    • December 30, 2017 at 7:36 pm

    where should i paste this code ? inside function.php (theme) or else ?

  • It would usually go in your functions.php file. I like to keep it in a separate file and include that file in my functions php file just because of the amount of code and to make it easy to find. I have also been know to build custom location rules into a plugin when the situation warrants.

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

You must be logged in to reply to this topic.

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.