Support

Account

Home Forums Feature Requests Post Category Parent Location Rule

Solving

Post Category Parent Location Rule

  • I love this plugin!

    One feature I’d like to see is the ability to select a post category parent under location rules. This way I don’t have to train users to remember to always select both the parent and child categories or keep adding rules whenever I/they add a child category.

    Thanks!

    • Elliot

    • January 16, 2014 at 9:43 am

    Hi @andrewmowe

    Thanks for the feature request. I’ll add this one to the to-do list.

    Cheers
    E

  • Great, thanks @elliot!

  • first of all, congratulations for the plugin, it is truly awesome

    This is the functionality I’m looking for. I tried to do it by location rules, but my skills are not enough

    For what date have expected this update?

    regards from Spain

  • Really god news. Iā€™m waiting for this update! This is what I need šŸ™‚

    • arudd

    • April 15, 2014 at 2:24 am

    Excited about this update!

  • Hi,

    If you are looking for a temporary solution until the update, here:

     

    functions.php

    add:

    /**
     * Tests if any of a post's assigned categories are descendants of target categories
     *
     * @param int|array $cats The target categories. Integer ID or array of integer IDs
     * @param int|object $_post The post. Omit to test the current post in the Loop or main query
     * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
     * @see get_term_by() You can get a category by name or slug, then pass ID to this function
     * @uses get_term_children() Passes $cats
     * @uses in_category() Passes $_post (can be empty)
     * @version 2.7
     * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
     */
    if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
        function post_is_in_descendant_category( $cats, $_post = null ) {
            foreach ( (array) $cats as $cat ) {
                // get_term_children() accepts integer ID only
                $descendants = get_term_children( (int) $cat, 'category' );
                if ( $descendants && in_category( $descendants, $_post ) )
                    return true;
            }
            return false;
        }
    }

     

    wp-content/plugins/advanced-custom-fields/core/controllers/location.php

    Line 566-569 to:

    if( in_array($rule['value'], $terms) || post_is_in_descendant_category( $rule['value'], $options['post_id'] ) )
    {
        $match = true; 
    }
  • I’m very interested in this feature, too!

    I tried a lot of code found in this support forum (i.e. this), and elsewhere (i.e. this, and this) so far with no luck.

    @hayatbiralem: thanks for your help! However, editing the plugin’s core files is not future-proof (whenever the plugin gets updated, you’ll lose your custom code).

    • Sagi

    • May 16, 2016 at 6:49 pm

    Is this feature still in the works? As i don’t see it implemented yet, and it would greatly enchance setting rules in this great plugin.

  • I have added a post category ancestor location rule to my filters and functions, not exactly a parent category rule, but it does the same thing. It will not match on the category so if you want it to match the category or any of its decedents then you need to set up both in the location rules. Hope this helps. https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-post-category-ancestor-location-rule.php

  • Hey John,

    That filter is great. Is almost what I was looking for (I’d personally prefer to match “also” the category) but I think I’ll keep it like that for now.

    Thanks a lot!

  • It would be easy enough to modify to do category & descendents, category already exists though so you can use category == OR category ancestor == in the location rules.

    • pagol

    • December 24, 2017 at 6:05 am

    i want to this feature too.

    i want to make rules 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.

    • pagol

    • August 11, 2020 at 6:35 am

    any update for it. i am facing still issue. this is long ago request

Viewing 14 posts - 1 through 14 (of 14 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.