Support

Account

Home Forums Backend Issues (wp-admin) Exclude particular page id's from relationship field selection

Solved

Exclude particular page id's from relationship field selection

  • Hi 🙂

    I did do a forum search however I’m not understanding how I can remove particular pages or posts from the selection options for a relationship field.

    Use case – I have a number of pages that are not indexed or searchable such as subscription thank you page, paypal payment error page etc.
    I do not want these to show up in the selection list for editors etc to select when they are using the relationship field.

    Any help appreciated, thanks 🙂

  • you may can do that with a workaround.
    if you can add a taxonomy (default active, for new posts)
    add that to all existing posts that should shown at relationship field.

    now you can use the filter taxonomy inside the relationship field (set it to this new created taxonomy)
    with that only post that have this taxonomy shows up.

    hope that help to solve your problem

  • Hello 🙂

    This is for static pages.

    is it possible to somehow use something along lines of

    ['post__not_in'] = array( 1994, 1999, 1979, 4201, 1989 )

    Thanks 🙂

  • you can try something like this:

    /**
     * Exclude current and static post/page from relationship field results
     */
    
    // 1. Add the name=[NAME_OF_RELATIONSHIP_FIELD].
    add_filter('acf/fields/relationship/query/name=my_relationship_field', 'exclude_id', 10, 3);
    
    // 2. Add the $field and $post arguments.
    function exclude_id ( $args, $field, $post ) {
    
        //3. $post argument passed in from the query hook is the $post->ID.
        $args['post__not_in'] = array( $post, 1994, 1999, 1979, 4201, 1989 );
        //should exclude post you edit, and posts with id 1994, 1999, 1979, 4201, 1989
        return $args;
    }
  • That did it!

    had tried to do it without the $post in the array and it did not want to play nice.

    Thank you,
    your help has been most appreciated!

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

You must be logged in to reply to this topic.