Support

Account

Home Forums ACF PRO Exclude Posts From Relationship Field

Solving

Exclude Posts From Relationship Field

  • Hello,

    I created a Relationship field which displays a list of all post types which are “Food”.

    I would like the user to be able to select any Food post, except for the Food posts with an ID of 1, 2, 3.

    How can I exclude Food posts from the Relationship field.

    This is what I tried:

    function my_relationship_query( $args, $field, $post )
    {
    $exclude_ids = array( 4518, 4516 );
    $args[‘post__not_in’] = $exclude_ids;

    return $args;
    }
    add_filter(‘acf/fields/relationship/query’, ‘my_relationship_query’, 10, 3);

  • HI @acfcfs

    I think that should do the trick.. with the correct IDs

    
    function my_relationship_query( $args, $field, $post ){
    	$args[‘post__not_in’] = array(1,2,3);
    	return $args;
    }
    //change yourfieldname to your specific field.. so you're not messing with any relationship field
    add_filter(‘acf/fields/relationship/query/name=yourfieldname’, ‘my_relationship_query’, 10, 3);
    

    The $args is the very same as what’s being used in WP_Query http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

  • Did you place it in your themes functions.php?

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

The topic ‘Exclude Posts From Relationship Field’ is closed to new replies.