Skip Navigation

[Resolved] Get parent post id for a "create form" post form

This support ticket is created 4 years, 2 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 14 replies, has 2 voices.

Last updated by Vane 4 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1484509

Hi,

I'd like to preselect a parent post value in a create type post form.

I have a working solution created years ago, what I want to use on the new site by copying and modifying it,
but it doesn't work.
It was created long years ago, with very different Toolset versions...

It allows to create Stories, related to Places CPT and related code looks like this:

so this does work:
in a content template:

[wpv-conditional if="( NOT(empty('[wpv-current-user info='role']')) )"]
  <a href="/create-story/?parent_place_id=[wpv-post-id id="$place"]">
    Create a new story
  </a> 
[/wpv-conditional]

in form definition:

<div class="cred-group cred-group-parents">
<div class="cred-field cred-field-_wpcf_belongs_place_id">
<label class="cred-label">
[wpml-string context='cred-form-Create Story Form-239' name='place Parent']The place this story belongs to:[/wpml-string]
</label>
[cred_field field='_wpcf_belongs_place_id' value='']
</div>	
</div>     

So I modified it but it doesn't fetch the correct parent CPT ID
(this case there is a Brand and its related Brand Posts):

so this does NOT work:
in a content template:

[wpv-conditional if="( NOT(empty('[wpv-current-user info='role']')) )"]
<a href="/create-brand-post/?parent_brand_id=[wpv-post-id id="$brand"]">Create a New Post</a> 
[/wpv-conditional]

in form definition:

	<div class="form-group">
		<label>[wpml-string context='cred-form-' name='Related Brand']Related Brand[/wpml-string]</label>
		[cred_field field='@brand-brand-post.parent' class='form-control' output='bootstrap' select_text='--- Must select ---']
	</div>

This second version adds the post id of the children (Brand Post), instead of the parent (Brand) to the URL, therefore it doesn't select the required related Brand.
The only difference I want to modify compared to the source solution, that in source it selects parent entity and doesn't allow to modify, change it,
on the new site I want to prefetch that value but allow the user to change it.

You must be logged in to test this feature at eg.:
***/brand-post/a-story-about-aqua/

I checked
[cred_field]
shortcode docs at:
https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_field
but I couldn't figure the solution out.

Thank you!

#1485087

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Your original solution will have been using Types 2, which stored post relationship data very differently than does Types 3, and the form shortcodes that you may have been able to use then were deprecated with the change to Types 3 and the corresponding updates to Forms.

Looking at what you are aiming to do, it is simply to link to a form to create a child post where the parent post is pre-selected (but can be changed). Is that correct?

That is described in the documentation here: https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/

Does that cover your use-case, or are you actually aiming to do something different?

#1486481

Hi Nigel,

my use case is a 3rd, compared to the linked docs 2 use cases.
In form editor I reverted back from Expert Mode, that modified the form content, so I re-added the Brand - Brand Post Relationship into that form, set it up, saved, but it:
- didn't solve the original issue ( user still have to select parent post (Related Brand)) and is not pre-selected
- it created new display issues for Post Title and Post Content fields (shortcode interpretation issues)
...so I copied back the original shortcode in Expert Mode.

I don't see any option to select the setting I want in Basic Mode, that's why I tried Expert Mode, hoping for relevant arguments.
I repeat what I want:
- allow user to select Related Brand (parent, 1st use case in linked doc),
- but preselect (set it in dropdown selector) the existing Related Brand, (like in 2nd use case, but not hiding selection dropdown).

Pls. remember:
"This second version adds the post id of the children (Brand Post), instead of the parent (Brand) to the URL, therefore it doesn't select the required related Brand."

I haven't mentioned yet, but I have a warning on frontent, where I test this Create Form:
"Could not set the parent post because it has the wrong type. The parent for this post should be of type brand."
Strange, because we can search in the Brand values (in dropdown), so the value options are correct, but it can not set the parent's one...

The relevant shortcode

[cred_field field='@brand-brand-post.parent' class='form-control' output='bootstrap' select_text='--- Must select ---']

is not added by coding it, but it is converted by Toolset Forms when I changed originally from Basic to Expert Mode,
so I don't understand the warning bolded above.

Maybe I didn't use parent-children words properly.
I name in a one-to-many relationship the first as parent and the last as children, so my Relationship of:
Brand -> Brand Posts
is totally equivalent with the sample in your linked doc of:
Venue -> Events,
so it should be good...

Thank you

#1486485

Maybe it helps more:
***/create-brand-post/?parent_brand_id=168
this number is the Brand Post's ID, but it should be the related Brand's ID!

Maybe
parent_brand_id
is incorrect?

I get it from the source website (Types 2)
from the 1st code in OP:

 
<a href="/create-story/?parent_place_id=[wpv-post-id id="$place"]">
    Create a new story
  </a> 

and I changed
parent_place_id
to
parent_brand_id
which might not be correct in Types 3

#1486683

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2020-01-30 at 11.59.06.png

Your use case is the same as that described in the documentation, it's just that you are not hiding the parent selector with CSS, because you want the user to be able to change the parent.

To confirm, you have a parent << child relationship of brand << brand post.

The form is to publish a brand-post post and should include a parent brand selector which is pre-selected.

Please create a new form to publish the child brand-post and it will add the parent selector field automatically. If it doesn't stop there, because there would be some problem with your relationship that means the form doesn't recognise it.

Assuming it does, you can switch to expert mode to examine the shortcode generated for the parent field, but there should be no need to make any changes to it.

Add the form to some page.

Next, go to where you want to display the link to add a child post (presumably, the template for the parent posts, or the output of a View displaying parent posts).

Use the Toolset Forms button to insert the link to publish a child post, which shows a dialog as per my screenshot, where you specify that the value is set according to the currently displayed content (i.e. the current parent brand post).

That will insert a shortcode like the following which generates the link:

[cred_child_link_form form='142' parent_id='-1' text='Create new task' target='_self']

That should be all there is to it...

#1486701

- I've already tried the shortcode:
[cred_child_link_form form='142' parent_id='-1' text='Create new task' target='_self']
you provided (changing form ID), it doesn't work, same problem.

- relationship works elsewhere properly, you can check it eg. here:
***/brand/aqua/
***/brand-post/a-story-about-aqua/

Pls. switch to private mode, let me give you admin credentials so you could test, create new brand post.

#1486711

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Sure, let me set a private reply so I can take a look.

Please confirm

1. the form
2. the page it is inserted on
3. the template (or View) where you are adding the link

#1486821

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I think I see the problem.

You haven't added the form to publish brand-posts to a page, which is required if you want to link to that form from a parent post (the template for brands or a View displaying brands).

The attributes on the cred_child_link_form shortcode are slightly confusing, because the form attribute is the id of the page where the form is inserted, not the id of the form. It cannot be a moving target, it needs to be a page.

So there are two scenarios where you have a form to add child posts.

You can include the form directly when displaying the parent post, i.e. you insert the form into the template for the parent posts. In that case you can pre-select the parent by adding the shortcode wpv-post-id as a value attribute to the parent select field in the form, which will pick up the id of the post where the form is displayed.

Or you can link to the form, in which case the form must be inserted into a page, and then linked to using the cred_child_link_form shortcode.

That's the scenario I was describing above in https://toolset.com/forums/topic/get-parent-post-id-for-a-create-form-post-form/#post-1486683

#1487093

Sorry, my bad, I misunderstood your Q2.
I certainly have a page where form is inserted on, its slug is:
create-brand-post

So according to your comment, I selected this page's ID ( 236)
and changed it in the code to:

[cred_child_link_form form='236' parent_id="-1" text="Create a New Post" target="_self"]

Pls note using this code URL parameter is:
parent_brand-post_id
in
***/create-brand-post/?parent_brand-post_id=***

So in both cases, also with code:

<a href="/create-brand-post/?parent_brand_id=[wpv-post-id id=" $brand"]"="">Create a New Post</a>

exactly the same issue appears, Brand is not pre-selected.

BTW I think argument
form='236'
is not " slightly confusing" but "obviously misleading"

Pls. also note using shortcode in {{}} form instead of [] drops error.
( I got that from here:
https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_child_link_form
from example )

#1488239

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I tested visited the create brand post page directly with a manually appended URL parameter ?parent_brand_id=157 and the expected Swedish giant was pre-selected in the dropdown, so that part works.

I'm not sure where you are inserting your links to the page to add a brand-post are, though, or what your intention is.

The parent post type is "brand". The child post type is "brand-post". The form publishes "brand-post" child posts and the parent should be pre-selected.

So that tells me the link should be shown on "brand" posts.

I edited the template for brands and don't see the link, so I added it.

(Note that the current Forms block doesn't include support for adding the various links, I used a Classic block and the Toolset Forms button there to insert the link.)

I then visited the Swedish giant on the front-end, saw the link, clicked it, and up came the form with the correct parent pre-selected.

So it works, I think there is some misunderstanding about where you want the link to work from.

#1488367

I know you have multiple support issues and may forget some details after days (like I used to do), so I summarize what I've written:

in original post (OP):
- This second version adds the post id of the children (Brand Post), instead of the parent (Brand) to the URL, therefore it doesn't select the required related Brand.
- I want to prefetch that value (related brand) but allow the user to change it.

later:
"I repeat what I want:
- allow user to select Related Brand (parent, 1st use case in linked doc),
- but preselect (set it in dropdown selector) the existing Related Brand, (like in 2nd use case, but not hiding selection dropdown).
- "I haven't mentioned yet, but I have a warning on frontent, where I test this Create Form:
"Could not set the parent post because it has the wrong type. The parent for this post should be of type brand."
Strange, because we can search in the Brand values (in dropdown), so the value options are correct, but it can not set the parent's one..."
- "***/create-brand-post/?parent_brand_id=168
this number is the Brand Post's ID, but it should be the related Brand's ID!"

in private:
"3. the template (or View) where you are adding the link:
ID: 231, Brand Post Form Template,
inserted into
ID: 170, Template for Brand Posts"

so they are in content templates!

My use case is almost the same as shown here:
https://toolset.com/wp-content/uploads/2019/02/toolset-form-for-child-posts-with-parent-selector.png
and this is in the Events content template (not the Venue)
Analogy:
Venue << Events
Brand << Brand posts
and you added your link into the Brands CT but it must be in Brand Post CT (analogy: Events)

I'm sorry I disagree: if you manually add the correct post ID to URL parameter, and that selects the correct Brand that doesn't mean it works.
It works if Toolset code fetches the correct related brand ID and adds that to URL parameter ( like it does on my source site created with Types 2 in 2015 )

#1488511

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Sorry, but the entire workflows for adding child posts using a form with a parent selector are predicated on the use case that you are looking at a parent post and you want to add a child post to that parent post, and that is what I've been trying to set up.

I did see your private comment about the template you had added this to, but understood that you were adding it to the wrong place as it wasn't clear from your earlier comments that the starting point for inserting this link is not the parent post but is a child post.

So what you are aiming for is not "add a child post to this parent post" but rather "add another child post to the parent of this child post", that would be correct, yes?

If that's the case then the correct way to get the parent id is with the item attribute, e.g.

[cred_child_link_form form='236' parent_id='[wpv-post-id item="@brand-brand-post.parent"]' text='Add another brand post' target='_self']
#1488779

Sorry, but I hoped after 3 days we are far more ahead - but I was wrong.

I clearly described what I want, multiple times, with code attached, so I went toward child post link shortcode is because
1) I tried / tested all ways I supposed to work and found this code axample in Toolset docs
2) you later also directed me towards that way.

Your code sent in your last reply also does not do what I want:
it pre-selects the parent entity (brand) though, but doesn't allow to modify it (and search in search area).
It is why I emphasized my way is a 3rd way of operation compared to the 2 described in the docs you linked)

I'm totally disappointed that we needed 3 days to figure out that I can't do what I want, what is described also in OP:
"The only difference I want to modify compared to the source solution, that in source it selects parent entity and doesn't allow to modify, change it,
on the new site I want to prefetch that value but allow the user to change it."

I give up, pls. assign ticket back to me, I reconsider it in the next hours and I may abandon my planned way of operation...

#1488815

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Your descriptions may have been clear to you, not to me I'm afraid.

I got sidetracked because it wasn't clear that you are using a different workflow to the prescribed workflow of adding a child post from a parent, and instead want to add another child post to the same parent from some existing child post. (At least, I think that is what you want, you haven't confirmed it.)

Anyway, now that I check, although the select dropdown is added to the form with the parent pre-selected (i.e. it appears to work), I see that it's impossible to choose a different parent than the one selected, if you type another parent post title it will show no items found.

That's evidently just how it works, if you pass the parent_cptslug_id parameter to the form it will pre-select the parent and won't let you change it, I hadn't come across that before.

So, we'll need a different solution.

Checking, I notice that if you edit the form, specifically the shortcode that adds the parent select field, and include a value attribute for some parent post ID, it will pre-select the parent and—importantly—it will still let you change the parent to something else, so this looks to be the route you will need to take.

Instead of using the cred_child_link_form shortcode to insert the link, construct the link manually (as you were doing previously), and this time add a URL parameter (that is different to the parent_brand_id parameter the form is listening for, maybe just 'parent'), where you generate the parameter using the wpv-post-id shortcode with the item attribute to specify the parent brand post, e.g.

?parent=[wpv-post-id item='@brand-brand-post.parent']

So when you click your link to the page with the form to add child brand-post posts it should includes a parameter such as ?parent=42.

Now edit your form, and update the shortcode for the parent selector field so that the value attribute gets the parent id from the parent parameter using the wpv-search-term shortcode, e.g.

value='[wpv-search-term param="parent"]'

You should hopefully find the same as I have on my local test site, that specifying the value attribute still allows you to change the parent.

#1488887

Your latest idea works, thank you! 🙂

So summarizing it for others:
1) I modified the Relationship field in my form's shortcode as:

[cred_field field='@brand-brand-post.parent' value='[wpv-search-term param="parent"]' class='form-control' output='bootstrap' select_text='--- Must select ---']

2) I modified my content template's relevant content as:

<a href="/create-brand-post/?parent=[wpv-post-id item='@brand-brand-post.parent']"="">Create a New Post</a>

Pls. note it works only on brand post pages (containing brand post content template), I haven't inserted this content template yet into brand CT and into some views.

It was hard but we got it, thank you!

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