Setting a Default Value for a Relationship Field

Support MB Relationships Setting a Default Value for a Relationship Field

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #41282
    Christie WalkerChristie Walker
    Participant

    I'm hoping this is the best place to post this. I'm using both Relationships and Frontend Submissions but I think the possible bug is in the main Metabox plugin.

    I have a relationship between 2 CPTs and I want to set a default value for this field when it's displayed on the front end. (It's ok to have the default value on the admin side too, but I'm not worried about that.)

    The value, however, is not a static value. It is based on the user that is currently logged in, and then some additional processing is done to get the actual value that should be the default.

    So I'm using the rwmb_normalize_{field_id}_field filter to assign $field['std'] to the proper value. That all works fine.

    The problem comes in with the RW_Meta_Box->is_saved function. When it calls raw_meta, the value returned is an empty array (because it's a relationship field? I'm not sure). So this check in is_saved fails:

    if ( false === $value ) {
        continue;
    }

    and this check returns true because the field is single but value is an empty array:

    if (
    	( $single && '' !== $value )
    	|| ( ! $single && is_array( $value ) && [] !== $value )
    	) {
    		return true;
    }

    is_saved returning true means that the $field['std'] setting is ignored because MB thinks the field has already been saved.

    So, I don't know if is_saved needs to be fixed to handle relationships better, or if there's some other method I should be using to accomplish this.

    Thanks!

    #41295
    PeterPeter
    Moderator

    Hello Christie,

    Thank you for your feedback.

    I do not see the default value std works with the field in the relationship metabox. I'm asking the development team about this issue and get back to you later.

    #41550
    Christie WalkerChristie Walker
    Participant

    Any updates on this?

    Thank you.

    #41559
    PeterPeter
    Moderator

    Hello,

    Thanks for following up.

    Our development team is working on this issue. I will let you know when I have any infomration.

    #43963
    Christie WalkerChristie Walker
    Participant

    Hello,

    It has been several months now. Any updates?

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.