Skip Navigation

[Resolved] set value for generic select or multiselect field in form for M2M relationship

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Minesh 4 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2705684

Hi there

I am hoping this is possible!I have two custom post types in a many to many relationship. When the user creates the 'child' post I want the form to set and save the value of a parent using a generic select field and a view to get the parent.

I used these excellent instructions and this all works very well:
https://toolset.com/forums/topic/how-to-pre-select-more-than-one-parent-on-a-child-post-submission-form/

I have this in the form:

[cred_generic_field type='select' field='folderforcurrentuser' display='select' single_select='true']
{
"required":1,
"validate_format":0,
"persist":1,
"default":"",
"options":[ [wpv-view name="folder-of-current-user-for-message-form"] ]
}
[/cred_generic_field]

Then this in functions.php:

add_action('cred_save_data', 'ff_set_multiple_related_folders_currentuser',10,2);
function ff_set_multiple_related_folders_currentuser($post_id, $form_data) {
  $forms = array( 36593 );
  $relationship_1_slug = "folder-message";
  $ms_field_slug = "folderforcurrentuser";
  if ( in_array( $form_data['id'], $forms ) )
  {
    $ms_selections = $_POST[$ms_field_slug];
    foreach($ms_selections as $ms_selection) {
      toolset_connect_posts( $relationship_1_slug, $ms_selection, $post_id);
    }
  }
}

The form successfully displays the select field and gets the parent post as the option but the user still has to select it... If the user selects it the relationship is also saved successfully. So everything works!

I just can't figure out how to get the form to set the value so the user doesn't have to.

Hoping this is a simple one! 🙂

Rita

#2705735

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The form successfully displays the select field and gets the parent post as the option but the user still has to select it... If the user selects it the relationship is also saved successfully. So everything works!

I just can't figure out how to get the form to set the value so the user doesn't have to.
===>
Do you mean you what to set current post where the form is displayed as the parent post of your child and with the parent drop-down select you want to set the current post as the default selected option?

#2705738

Hi Minesh
Yes. Exactly. Actually I literally just this minute figured it out. I need to add a default with a view that gets the parent post's ID only. So I now have this:

View to get the parent id

<wpv-loop>
        [wpv-post-id]
</wpv-loop>

View to get the same parent but as select options

<wpv-loop>
          [wpv-item index=1]
          {"value":"[wpv-post-id]","label":"[wpv-post-title]"}
          [wpv-item index=other]
          ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>

Cred form to create a new child post

[cred_generic_field type='select' field='folderforcurrentuser']
{
"required":1,
"persist":1,
"default":[ [wpv-view name="folder-id-current-user" tabslug="current"] ],
"options":[ [wpv-view name="folder-generic-options-for-users-current-new-message"] ]
}
[/cred_generic_field]

Function to create the many-to-many intermediary and set the parent

add_action('cred_save_data', 'ff_set_related_folder_currentuser',10,2);
function ff_set_related_folder_currentuser($post_id, $form_data) {
  $forms = array( 36593 );
  $relationship_1_slug = "folder-message";
  $ms_field_slug = "folderforcurrentuser";
  if ( in_array( $form_data['id'], $forms ) )
  {
    $ms_selections = $_POST[$ms_field_slug];
    foreach($ms_selections as $ms_selection) {
      toolset_connect_posts( $relationship_1_slug, $ms_selection, $post_id);
    }
  }
}

So now I have a new problem... The parent is selected in the cred form but now the relationship is not saved when the child post is created... so the function is not working anymore.

What do you reckon? 🙂

#2705740

Minesh!
I got it. I have a SELECT field in the cred form but the function is expecting a MULTISELECT field. So I changed the cred form generic field to a MULTISELECT field and it works. Problem solved!

[cred_generic_field type='multiselect' field='folderforcurrentuser']
{
"required":1,
"persist":1,
"default":[ [wpv-view name="folder-id-current-user" tabslug="current"] ],
"options":[ [wpv-view name="folder-generic-options-for-users-current-new-message"] ]
}
[/cred_generic_field]
#2705745

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok great - you're welcome to mark resolve this ticket.

#2705746

Thanks for your time Minesh. All is well.