[Resolved] How set a relationship field default value via PHP
This support ticket is created 2 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.
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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Hello,
i am trying to set a default value for a relationship field added to a post form by using the cred_filter_field_before_add_to_form filter.
I used successfully this filter when i needed to set a default value for a custom field (textfield) but i don't find a way to do the same with a relationship field (adding a value to default_value attribute seems useless).
How can this be accomplished?
Thanks for the help.
So that I understand the requirements correctly, is it that each form will have a different default parent value ?
Reason for the question is because there is a default value option on the form for you to set the ID of the parent post that you would want to be the default parent. See Screenshot.
Thanks for the answer.
I need to set the default value via PHP.
Just for example, imagine that i need to set a random default value (obviously from al the options of the select) each time the page is loaded.
I did a quick test on this one for you with a custom shortcode providing the value attribute and it works.
What you can do is to write a custom shortcode using the shortcode generator at the link below. hidden link
At the end you should be returning a post ID from the parent post type. Once you've done this just add the custom shortcode to the value attribute of your relationship field and it will automatically select the parent post.