Support

Account

Home Forums Backend Issues (wp-admin) Location Rules: Post Status?

Solved

Location Rules: Post Status?

  • Would it be possbile to add an ACF location rule for post status? Or would someone know what code I could add through functions.php or a plugin to get the functionality of having such a location rule?

    The question is prompted by the fact that I have an ACF repeater field that I’d like to only show in the editor for posts being created by Contributors, but I’d also like the Admins reviewing the post for publication to see them (so the ideal combination of location rules would be: user=contributor or post-status=pending).

    My current solution is to just enable the field for both Contributors and Admins (and then tell the Admins to use their Screen Options to hide the field until they need it for reviewing a Contributor’s post).

  • Hi @gato_gordo

    Thanks for the request.

    Yes, it is quite easy to create a custom location rule. You can follow this guide to do so:

    http://www.advancedcustomfields.com/resources/tutorials/custom-location-rules/

    I’ll also add this request to the to-do as I would like to see it in the core over the next coming versions

    Thanks
    E

  • Thanks! Using that tutorial, I came up with this quick and dirty custom rule:

    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    function acf_location_rules_types( $choices )
    {
        $choices['Post']['post_status'] = 'Post Status';
        return $choices;
    }
    
    add_filter('acf/location/rule_values/post_status', 'acf_location_rules_values_post_status');
    function acf_location_rules_values_post_status( $choices )
    {
        $post_status = array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash');
        $num_status = count($post_status);
     
        for ($index = 0; $index < $num_status; $index++)
        {
            $choices[$post_status[$index]] = $post_status[$index];
        }
     
        return $choices;
    }
    
    add_filter('acf/location/rule_match/post_status', 'acf_location_rules_match_post_status', 10, 3);
    function acf_location_rules_match_post_status( $match, $rule, $options )
    {
        $this_post_status = get_post_status($options[post_id]));
        $selected_post_status = $rule['value'];
       
        if($rule['operator'] == "==")
        {
          $match = ( $this_post_status == $selected_post_status );
        }
        elseif($rule['operator'] == "!=")
        {
          $match = ( $this_post_status != $selected_post_status );
        }
     
        return $match;
    }

    Ideally, you’d wouldn’t have to hard-code the array of statuses (in case they change or custom ones have been created), but quick googling didn’t turn up a WordPress construct that returns an array of defined post statuses.

  • Hi @gato_gordo

    Thanks a million for the code.

    I have taken from and improved on it, and now it is part of the next version!

    You can find it on github currently if you wish to test it out.

    Thanks again,
    Elliot

  • This was in the latest patch notes but I don’t see it, is it coming in a future update?

  • Hi @shauny

    You should find the post_status in the location rules under post

    Cheers
    E

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

The topic ‘Location Rules: Post Status?’ is closed to new replies.