Support

Account

Home Forums ACF PRO Hide Private Posts in Relationship Field Loop

Solving

Hide Private Posts in Relationship Field Loop

  • I have a relationship field loop and am trying to prevent private posts from being shown in it. I’d like to be able to add private posts, just not show them in the loop until they’re made non-private. Below is the filter I’m using but it doesn’t seem to do anything. I’m not sure if it matters, but this relatiionship filed is a subfield of a repeater.

    function eco_relationship_query( $args, $field, $post )
    {
        // remove private posts
        $args['post_status'] != 'private';
    
        return $args;
    }
    // filter for a specific field based on it's name
    add_filter('acf/fields/relationship/query/key=field_54e7615a62af4', 'eco_relationship_query', 10, 3);
  • Update: the relationship field seems to show posts no matter what their post_status is. Even drafts show up in the loop (on the published page). Is there a way to filter out everything except for published posts?

  • Hi @jmccall,

    Thanks for the post.

    I would recommend you pass the private post’s ids with a ‘-‘sign as a prefix to exclude them from the search.

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

The topic ‘Hide Private Posts in Relationship Field Loop’ is closed to new replies.