Skip Navigation

[Resolved] Assistance converting to new post relationship model

This support ticket is created 5 years, 1 month 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 18 replies, has 4 voices.

Last updated by Beda 5 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#1206721

I am preparing to convert to the new approach to the post relationships to take advantage of new features... specifically to be able to set multiple parents for a new set of post-types we are implementing...

I have run the backwards compatability test and come up with a comparatively small number of (relevant) issues that may need to be addressed prior to the migration.

In my theme functions.php file I have the following code including wpcf_belongs:

//validate if specific form
if ($form_data['id']==1564)
{
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) and !empty($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
if (!isset($_POST['_wpcf_belongs_company-profile_id']) or (isset($_POST['_wpcf_belongs_company-profile_id']) and $_POST['_wpcf_belongs_company-profile_id']==""))
{
//set error message for my_field
$errors['_wpcf_belongs_company-profile_id']='To make an announcement you must be logged in, authorised and make the announcement via the "Make an Announcement" button on your company profile';
}
}
}

This relates to a CRED form which enables people who are logged in and authorised (by having their email address in a field in a company profile to make an announcement on behalf of the company...

I am unsure of what changes I need to make to make this compatible... could you please advise?

I also have a number of CRED forms with backward compatability issues... including the form 1564 referred to in the functions.php file... all which are quite similar.

[cred-generic-field field="_wpcf_belongs_company-profile_id" type="hidden" class=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"[wpv-search-term param='parent_company-profile_id']"
}
[/cred-generic-field]
[cred_show_group if="($(_wpcf_belongs_company-profile_id) eq '' )"]
NOTICE: To make an announcement you must be logged in and authorised and make the announcement via the "Make an Announcement" button on your company profile or the My Profiles page.
[/cred_show_group]
[cred_show_group if="($(_wpcf_belongs_company-profile_id) ne '' )"]
Company Name: [cred-post-parent get='title']
<div class="cred-field cred-field-_wpcf_belongs_company-profile_id">
</div>

I am also unclear as to what I need to do here... could you perhaps help.

With your guidance on these I am moderately confident I can deal with the rest of the issues. Also I am unclear if I need to fix these prior to migrating or afterwards.

Thanks in anticipation

#1207124

Hi, we should go through this process on a staging environment first to confirm everything is working before deploying the changes to your live site. Here's the general idea. You should plan to put the site in Maintenance Mode shortly during the update process so there's no chance of Users submitting the Forms during the update. Then run the post relationships migration process. Then you will make changes in your functions.php file and update the CRED forms. Once all those updates are made, then you can turn off Maintenance Mode.

The changes you make in functions.php will depend on the new Post Relationship slugs, which you will be able to find once you run the post relationship migration process. Most likely it will be company-profile, but it could change. So your validation script will be updated to reflect that new field name in the $_POST and $errors arrays:

//validate if specific form
if ($form_data['id']==1564)
{
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) and !empty($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
if (!isset($_POST['@company-profile_parent']) or (isset($_POST['@company-profile_parent']) and $_POST['@company-profile_parent']==""))
{
//set error message for my_field
$errors['@company-profile.parent']='To make an announcement you must be logged in, authorised and make the announcement via the "Make an Announcement" button on your company profile';
}
}
}

The $_POST key format is now

'@' + post relationship slug + '_parent'

The $errors key format is now

'@' + post relationship slug + '.parent'

Notice these are slightly different, where there is an underscore in the $_POST key there is a dot in the $errors key.

The Form code seems okay except for one thing:

Company Name: [cred-post-parent get='title']

The cred-post-parent shortcode is deprecated. I'm not exactly sure how it was used in this case - is this Form used to create new posts or edit existing posts?

Also, there is a small bit of code that must be added to functions.php for any Form where you have used a generic field to set the parent post using "_wpcf_belongs_slug_id". This generic field will no longer be sufficient to associate parent and child posts in the new post relationships system. You must use the post relationships API to connect those posts in the backend when the form is submitted, if a generic field is used to select the parent post. I can help with this code if necessary, or we can recreate the Form so that it doesn't use generic fields. That will require some front-end design changes, since conditional groups will not work with standard parent post select fields.

#1207343

Christian

Thanks for your prompt response. I will have to do what you suggest and see how that goes.

You suggested a problem with the code:

Company Name: [cred-post-parent get='title']

This was used to display the parent company name (the title of the post) in the CRED form used by a company representative to make an announcement - a new post.

Here is a description of the relevant work flow...
1. The user would login.
2. the user would click on a button which was displayed to them on the basis that they were authorized by the company to make announcements on their behalf (their logged in email address matched the value in a field in the relevant company-profile post.)
3. the user would be taken to the CRED form for new company announcement, where the company name was displayed and the connection between the new announcement post and the parent company-profile was made.

I think I will need help with the functions.php file... let me do what you suggest first and get back to you.

Cheers

Peter

#1207714

Okay thanks for the explanation. In this case, you can replace the cred_post_parent shortcode with a wpv-post-title shortcode, and pass in the company ID from the URL parameter:

Company name: [wpv-post-title id="[wpv-search-term param='parent_company-profile_id']"]

That should show the name of the Company effectively.

Here's some documentation for connecting two posts using the post relationships API:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

You'll need to add a cred_save_data hook to trigger that code when the Form is submitted:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

I can assist if you get stuck somewhere in that code.

#1214057

Christian

Thanks so much for your advice so far on this.

OK I have set up on a test installation of my site and performed the migration .... it all went fine... some issues flagged in the technical details section of the migration... unfortunately I lost the details... cut and pasted into googledoc... which for some reason had an issue and I lost the contents.

that aside ... it all worked fine... I made no changes to the functions.php file or to the Cred forms and I tested the form flagged above... and it worked as before... I created a new post with the Cred form 1564... it automatically pulled the parent and populated the field so that a connection between parent and child was created when the post was published.

I then thought I would see what changes I could find in the database via doing an export using WPallexport... and things seemed to be unchanged... with the relevant post number of the parent recorded against the field: [_wpcf_belongs_company-profile_id] and nothing sorted as against [parent]... so it looks for all intents and purposes as if nothing has changed.

BTW I also looked at the functions.php file and it all looked as before.

What is going on?

I tend to live by the motto "if it ain't broke..." but not sure here I am concerned that if I don't make adjustments I may have issues with later plugin updates but I don't know.

BTW I then wen to have a look at what was involved in setting up for the new Funding Event Post type... the reason for the change to the new relationships... I edited the relationship for funding events such that each funding event could have multiple investor parents... So with that done I went to set up the CRED form to create a new funding event.... and struck a problem... the form did not provide the post type "funding event" as an option....

Not sure where I am going wrong.

Please help

Cheers

Peter

#1214250

things seemed to be unchanged... with the relevant post number of the parent recorded against the field: [_wpcf_belongs_company-profile_id] and nothing sorted as against [parent]... so it looks for all intents and purposes as if nothing has changed.
The new post relationships system uses its own special tables like wp_toolset_relationships and wp_toolset_associations to manage relationships, so if you're not looking at those tables it may appear that nothing has changed. In the Form, you currently use a generic field to capture the parent ID:

[cred-generic-field field="_wpcf_belongs_company-profile_id" type="hidden" class=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"[wpv-search-term param='parent_company-profile_id']"
}
[/cred-generic-field]

Since you haven't made any changes to the Form yet, that value will continue to be saved in the database postmeta table like before. You could remove the "persist" option in the generic field shortcode if you want to stop saving that value. Again, that's *not* how the new relationship is managed. In the new system, you cannot rely on the old postmeta key.

BTW I also looked at the functions.php file and it all looked as before.
The relationship migration process will not modify the functions.php file automatically, if that's what you were expecting. The expected behavior is that you go through the file yourself and make the necessary changes.

#1214332

Thanks for your reply christian.

So after the migration should existing posts with relationships have the new tables: "hidden link" populated... as I don't seem to be able to see them via wpallexport which has been my go to.

Or is it the case that when I have made the changes required I will need to reestablish the relationship between posts connected under the old system?

Also what are your thoughts on not being able to see "funding event" as an option when trying to set up a CRED form.

Cheers

Peter

#1214369

So after the migration should existing posts with relationships have the new tables: "hidden link" populated... as I don't seem to be able to see them via wpallexport which has been my go to.
You lost me a bit, as I'm not sure what that URL is supposed to represent in the context of what we're discussing. The new tables should exist in the database and the old posts should be referenced in the new tables. I don't think wpallexport is designed to look in those tables, so you won't be able to select them for export. We're drifting a bit off topic, so if you have more questions about exporting you should create a new forum ticket. Let's use this current ticket to discuss post relationship migration specifically.

Or is it the case that when I have made the changes required I will need to reestablish the relationship between posts connected under the old system?
No, that should not be the case. The post relationship migration tool goes through the database and converts the link between posts in old relationships to a link between the same posts in new relationships.

Also what are your thoughts on not being able to see "funding event" as an option when trying to set up a CRED form.
If the post type was created by Types, it could be a caching issue at the server level. Try purging any object caches in the wpengine settings page. Also check the post type settings in Toolset > Post types and confirm the post type options include show_ui and publicly_queryable. If none of these seems to resolve the problem, feel free to create a separate ticket to investigate this issue in more detail.

#1214662

Christian... sorry of course the URL doesn't make sense...

OK everything else is starting to make more sense now.

I have gone exploring in Phpmyadmin and figured it out.

Will try to stick to relationships migration...

So where we are at now:

I have added the code to the functions.php and it still works.. yay and the "company name" code for the CRED form and that also works...

in reply#1207124 you said the code in the CRED form was deprecated... Not sure what that means.

I have looked at the links provided in reply#1207714 and I am sorry but I am not a programmer and cannot make sense of that. the code I have within the Cred form was provided to me by your colleagues over a number of support requests.

Could you please suggest what changes I need to make to the CRED code to fit the new way?

Regarding the "funding events" post type... I had unselected publicly_queryable...now selected and working

Have also found and installed the new addon for WPallimport so should be able to connect funding events to company profiles now.

Thanks in anticipation

Peter

#1215501

in reply#1207124 you said the code in the CRED form was deprecated... Not sure what that means.
It means don't use the [cred-post-parent get='title'] shortcode anymore because it is no longer recommended. I recommended you use the wpv-post-title shortcode instead:

Company name: [wpv-post-title id="[wpv-search-term param='parent_company-profile_id']"]

Could you please suggest what changes I need to make to the CRED code to fit the new way?
I can provide some examples. Which Forms use a generic field to capture the parent post ID as _wpcf_belongs_slug_id? Please provide the numeric ID for each Form, and the parent and child post type slugs for each Form.

#1216717

Christian

I had already changed the "Company name" code snippet as recommended.

The first form I need to be able to fix to work the new paradigm is the CRED form #1564 addressed in this support request.

It is one of the more complex forms on my site as it is only shown to logged in users who are the author of the company profile parent post or who's email address appears in the company profile parent post.

The form then displays the title of the company profile and automatically establishes the parent-child relationship.

So there is the validation that needs updating so the parent-child relationship is set up properly and you also previously said that cred form will need front end design changes since conditional groups will not work with standard parent post select fields. the code in question is (from above):

[cred-generic-field field="_wpcf_belongs_company-profile_id" type="hidden" class=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"[wpv-search-term param='parent_company-profile_id']"
}
[/cred-generic-field]
[cred_show_group if="($(_wpcf_belongs_company-profile_id) eq '' )"]
NOTICE: To make an announcement you must be logged in and authorised and make the announcement via the "Make an Announcement" button on your company profile or the My Profiles page.
[/cred_show_group]
[cred_show_group if="($(_wpcf_belongs_company-profile_id) ne '' )"]
Company Name: [cred-post-parent get='title']
<div class="cred-field cred-field-_wpcf_belongs_company-profile_id">
</div>

The other form is form #31 with parent and child slugs being: "company-profile_announcement" where the offending piece of exiting code is:
<div class="cred-field cred-field-_wpcf_belongs_company-profile_id">
<label class="cred-label">
Company Name
</label>
[cred_field field="_wpcf_belongs_company-profile_id" value=""]
</div>

What I want to achieve is the form merely displaying the name of the parent and not giving any option to change this...
I tried:
Company Name: [wpv-post-title id="[wpv-search-term param='parent_company-profile_id']"]

but this gave me the title of the child not the title of the parent.

Could you suggest what the code should be to display the name of the parent with no option to change that name?

On a related note... while trying to figure this out I created a new form for "Funding Event" to figure out what code to replace this with. Here is what was created.

<div class="form-group ">
<label>company-profile_funding-event</label>
[cred_field field='@company-profile_funding-event.parent' class='form-control' output='bootstrap' select_text='--- not set ---']
</div>

When I viewed the new form to create a new "Funding Event" the this code generated a drop down of 10 company names from a list of over 3000 company profiles. Could you suggest how I can fix this so it picks up all companies? also with an autofil... preferably that does not show a list of all companies.

With assistance on the above. I think I can figure out the rest of what I need at this point.

I have an other couple of issues I will address in separate requests.

Thanks in anticipation.

Peter

#1217267

Here are the changes to the Form code:

[cred-generic-field field="generic_company-profile_id" type="hidden" class=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"[wpv-search-term param='parent_company-profile_id']"
}
[/cred-generic-field]
[cred_show_group if="($(generic_company-profile_id) eq '' )"]
NOTICE: To make an announcement you must be logged in and authorised and make the announcement via the "Make an Announcement" button on your company profile or the My Profiles page.
[/cred_show_group]
[cred_show_group if="($(generic_company-profile_id) ne '' )"]
Company Name: [wpv-post-title id="[wpv-search-term param='parent_company-profile_id']"]
[/cred_show_group]

Here are the changes to the validation code:

//validate if specific form
if ($form_data['id']==1564)
{
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) and !empty($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
if (!isset($_POST['generic_company-profile_id']) or (isset($_POST['generic_company-profile_id']) and $_POST['generic_company-profile_id']==""))
{
//set error message for my_field
$errors['generic_company-profile_id']='To make an announcement you must be logged in, authorised and make the announcement via the "Make an Announcement" button on your company profile';
}
}
}

but this gave me the title of the child not the title of the parent.
Please tell me the exact URL you were visiting when you tried this. In order to capture and display the correct parent post ID / post title, there must be a URL parameter parent_company-profile_id in the URL, and it must be a parent post ID. It looks like this was set up so that a link to this page contained that special URL parameter, which allowed you to predefine the parent post. If this is not working as expected, please copy + paste the search term shortcode outside the cred_show_group and wpv-post-title shortcode so we can test it.

When I viewed the new form to create a new "Funding Event" the this code generated a drop down of 10 company names from a list of over 3000 company profiles. Could you suggest how I can fix this so it picks up all companies?
Normally a post relationship field will generate a basic select input if just a few options are available. The system automatically switches to an autosuggest style dropdown field when more than a certain number of options are available, I think that number is 15 or 16. If that's not working for you, please create a separate ticket and we can discuss in more detail.

#1217355

My issue is resolved now.
Thank you!

#1217964

Were you able to figure out the post relationship code on your own? We didn't implement the cred_save_data hook yet, which is where you will establish the post relationship. Let me know if you'd like to reopen this ticket and continue.

#1218016

Christian

Thanks for following up.

It seems to be working fine and with no legacy code that I could see.

I created a new announcement.. the parent field was populated and the post relationship established... without the cred_save_data hook?

here is the code at the top of the CRED form

[credform class="cred-form cred-keep-original"]

[cred_field field="form_messages" value=""]

[cred-generic-field field="generic_company-profile_id" type="hidden" class=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"[wpv-search-term param='parent_company-profile_id']"
}
[/cred-generic-field]
[cred_show_group if="($(generic_company-profile_id) eq '' )"]
NOTICE: To make an announcement you must be logged in and authorised and make the announcement via the "Make an Announcement" button on your company profile or the My Profiles page.
[/cred_show_group]
[cred_show_group if="($(generic_company-profile_id) ne '' )"]
Company Name: [wpv-post-title id="[wpv-search-term param='parent_company-profile_id']"]
[/cred_show_group]

am i missing something?

Peter

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.