Support

Account

Home Forums General Issues Specifying default choices for relationship field

Solving

Specifying default choices for relationship field

  • I’ve got a relationship field for which I’d like to set up some default options as 90% of posts will have the same 3-4 choices specified.

    I’ve currently got the following code, which is supposed to only populate a list that doesn’t already have a value specified:

    add_filter('acf/load_value/name=ctas', function($value) {
    
        if(empty($value)) {
            $value = get_posts(array(
                'post_type'      => 'cta',
                'posts_per_page' => -1,
                'orderby'        => 'menu_order',
                'order'          => 'ASC',
                'fields'         => 'ids',
            ));
        }
    
        return $value;
    
    });

    This populates the right-hand side of the relationship field just fine, but unfortunately the choices are not saved with the post.

    Any pointers would be much appreciated!

  • OK, I think the problem with the relationship field choices was unrelated to my PHP code.

    It seems if I don’t specify a title for a post the post’s relationship field choices don’t save. Not sure if this is a bug or intended behaviour but thought I’d mention it just in case!

  • Hi @sam,

    Thank you for raising this issue.

    I believe this is the intended behavior for the Relationship field to know where it is saving the data to. The title has to be part of the array.

  • Hi @sam ! Sorry to relive this old topic..

    Can you share your workaround to solve this problem? I’m facing exactly the same difficulties.. Can’t find nothing similar to help

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

The topic ‘Specifying default choices for relationship field’ is closed to new replies.