Skip Navigation

[Resolved] How to auto-populate a relationship field in a post form?

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.


Tell us what you are trying to do?

- I have a custom post type called 'Reference'
- I have a User Role of 'Customer' and also a linked Custom Post Type of 'Customer' (the CPT is to allow me to store extra data on the Customer which I cannot store within the User profile). The 'post title' of the Customer 'Post' is the email address which the Customer 'User' signs in with. This is all fine so far.
- I have a 'Relationship' set up between 'Customers' and 'References' (a one-to-many relationship, so a Customer can input multiple References).
- On my site, 'Customer' users log in, and then they can input a 'Reference' (as long as they have at least 1 credit, but that credit part is not relevant to this thread, it's just for information purposes).
- When inputting a 'Reference', the Post Form on the front end has the Relationship field (customer-reference is the slug) setup as part of the Form, and as mentioned above, the Customer has to be logged in in order to access this form.

So, what I want to achieve is, for the value of the Relationship field 'customer-reference' to be pre-populated in my Post Form for 'Reference', with the logged in Customers email address. To try to achieve this, I input the following code into my 'Reference' Post Form:

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'customer' )"]
[cred_field field='@customer-reference.parent' class='form-control' output='bootstrap' required='true' author='$current' select_text="[wpv-current-user info='email']" value="[wpv-current-user info='email']"]

However, although this 'appears' to work, it actually does not. I will explain:

- When the form first loads up, the email address indeed 'appears' to be pre-populated in the relationship field (please see attached image '1-form-loads.jpg'). The logged in user email address is in the field, but it does look 'greyed out'.

- Then when I click to submit the form, the form does not submit, and instead my Relationship field displays an error message of 'this field is required', suggesting that the field is in fact empty, even though the email address is showing in there (please see attached image '2-submit-form-but-field-is-not-populate.jpg').

- So I then have to click on the dropdown box (for the customer-reference field), where I am given the email address to click and select, after which the value shows again, but this time it is not 'greyed out' (for this part, I recorded a quick screencast, which you can view on this link = hidden link). After doing this, this time when I click the submit button, the form submits.

So, my question is, how do I get the Relationship field 'customer-reference' to be pre-populated AND selected with the value of the current logged in user email address please? If you could also explain why the code I have used does not work properly (just so I can understand better) that would be great 🙂

Thanks in advance for your help,


Is there any documentation that you are following?

I could not find any documentation explaining how to do this

Is there a similar example that we can see?


What is the link to your site?

It's under development, I can provide a private link (and login credentials) if you need it, in a private window please 🙂


So, my question is, how do I get the Relationship field 'customer-reference' to be pre-populated AND selected with the value of the current logged in user email address please?
Hello, normally post relationship fields will display post titles for each option, but the value of each option is defined as the corresponding post ID rather than the post title. So in your case, I would not expect to be able to set the value of the relationship field using the email address. Instead, I would expect you need a way to get the corresponding post ID, and insert that post ID as the value of the relationship field instead of the email address.

This may require a bit of custom code, given your description of the current setup. A custom shortcode that returns the current User's Customer post ID would be useful here (and possibly in other areas of the site where the proxy post ID is needed). If the User is the author of his or her own Customer post, you may use legacy Views to create a View of Customer posts, filtered by post author, where the author is the same as the current logged-in User. Assuming each User can have only one corresponding Customer post, the results of this View will include the one corresponding Customer post.

With that View created, you could construct a custom shortcode that accesses the results of this View and returns the post ID of the corresponding Customer post. We have a PHP API available that can help you access information from the results of a View:

A custom shortcode that returns a Customer post ID from such a View might look something like this example:

add_shortcode( 'tssupp_current_customer_id', 'tssupp_current_customer_id_func');
function tssupp_current_customer_id_func($atts)
  $view_id = 12345;
  $results = get_view_query_results( $view_id );
  return isset($results[0]) ? $results[0]->ID : 0;

You would replace 12345 with the numeric ID of the View you create of filtered Customer posts.

To test the shortcode, you can place it on a test page temporarily and log in as any User with a corresponding Customer post. You should see the ID of their Customer post rendered in the page contents with this code:

Current customer id: [tssupp_current_customer_id][/tssupp_current_customer_id]<br />

If that's working correctly, register the shortcode name tssupp_current_customer_id in Toolset > Settings > Front-end Content in the "Third-party shortcode arguments" section, and you should be able to then use the shortcode instead of the email address shortcode in your current relationship field shortcode implementation, without specifying the select_text attribute:

[cred_field field='@customer-reference.parent' class='form-control' output='bootstrap' required='true' author='$current' value='[tssupp_current_customer_id][/tssupp_current_customer_id]']

Assuming the Customer post title is the same as the email address, it should be displayed automatically as the selected title.

Let me know if you have questions about implementing such a custom shortcode or using the legacy Views editor. If legacy Views is not enabled on your site currently, you can enable it by going to Toolset > Settings > General and selecting the Editing Experience option that enables both the Block editor experience and the legacy editor experience. Then you will see a main menu item for Views under Toolset.