[Resolved] Populate Child Post Title with Parent Post Title
This thread is resolved. Here is a description of the problem and solution.
Problem:
Client is using the cred_save_data hook to dynamically generate a child post title based upon the title of the parent post, how should they retrieve the correct parent post ID?
Solution:
This changed with Types 3, which no longer stores the parent post ID on the child post.
You would get the parent post ID using the toolset_get_related_post function of the new API.
I have forms where I don't want the user to be able to change the parent post when submitting a child post so the selector field is hidden. To avoid child post title that look like this: CRED Auto Draft 2d64c7b961563497c88ffc9e24fe0019, I use this hook to update the post title on the child post with the post title of the parent post:-
You mention two seemingly unrelated things in your question, or is it 2 questions?
* you hide the parent selector field so that the parent cannot be changed
- as before you would do this with CSS. Where you insert the relationship field give the wrapper div a class name you can target as required
* you use the cred_save_data hook to give the child post a title
- presumably this is required because you hide the title field in the form and don't want users to enter a title
The principles of this are the same before and after, but the specifics depends on your particular case.
Firstly, if you have an existing site and have updated to Types 3.0 but have not run the relationships migration, this will work exactly as before. If you *have* run the migration, it will also work as before, assuming you make no other changes to your forms, because there is a backwards-compatibility layer to ensure it does.
It becomes an issue when you are creating new forms that include relationship fields, for which you would need to use the new relationships API to get the parent post, specifically toolset_get_related_post, in place of using get_post_meta.
I'm doing a bit of both! It doesn't seem to get any easier to find what I'm looking for...
I'll have a look at the link you've provided and hopefully all will become clear. Right now I'm struggling to understand how we go about things now. I understand which relationship type/s I need to use and when but am unclear how we go about building things now. For the purposes of my question, please assume we're talking about a brand new install (not an existing site that's been migrated). Can you confirm whether the following statements are correct or not please?
1. If no relationship has been set up between post types, post forms should be created via Toolset > Post Forms > Create
2. If a relationship has been set up between post types, a Relationship form should be created
Not quite. I don't think we have communicated this very well.
If you want to set-up post relationships with a front-end form, for many-to-many relationships you can only do that with a "Relationship Form".
For one-to-many relationships you can do it the same as before, including a parent selector in a child post form, although the mark-up used for the parent field has changed from before (but you won't need to worry about that if you auto-generate the form).
I don't currently have any many-to-many relationships. I've noticed the new mark-up for the parent field and been creating forms as I always have done but either something's changed and I'm not doing it right anymore or there's a bug.
I've had a look at the link you provided but I'm struggling to translate my old piece of code as required. Can I take you up on your offer of further assistance please?
but it's updating with the title of my child post form. I'm so stressed out right now over the lack of good information for existing sites and the work involved in having to change themes without warning just as I was about to launch a new site, I just can't see the wood from the trees anymore. It's probably something really simple; I'd appreciate a strong pointer.............thanks Nigel
Can you do your magic please? I think once I see the general format for extracting the parent post elements, I'll be able to build on it (as I've done previously)
If you have changed the markup for your form and it is using a relationship that you defined at Toolset > Relationships, then the second argument for toolset_get_related_post should be the slug of the relationship rather than an array of the post types, which is intended for legacy relationships.
Try changing that argument to the relationship slug.
If that doesn't work let me know and I'll set up a test.
For that second one, you again need to replace the line $parent_post_id = get_post_meta(... with a call to get the parent post ID using toolset_get_related_post.
If you fix the first one by changing the array argument to the relationship slug then I think you should be able to fix this second one in a similar fashion, but let me know if you get stuck.