Support

Account

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

  • 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.