Skip Navigation

[Resolved] How set a relationship field default value via PHP

This support ticket is created 2 years, 9 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.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by konstantinosD 2 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#2317393

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.

#2317539

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2022-03-15 at 10.33.47 AM.png

Hi Konstantinos,

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,
Shane

#2317583

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.

#2317783

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Konstantinos,

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.

Please let me know if this helps.
Thanks,
Shane

#2318173

Thanks Shane,
i will try this evening and i will let you know.

#2318751

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Konstantinos,

Any update on this for me ?

Thanks,
Shane

#2323465

My issue is resolved now. Thank you!