Skip Navigation

[Resolved] Returning parent post title when saving NEW post with CRED form

This thread is resolved. Here is a description of the problem and solution.

Problem:
toolset_get_related_post() API is not working when using it in a cred_save_data() hook.

Basically, when we try to get the related post (paren) of the just created (child) post inside a cred_save_data() then toolset_get_related_post() does not return anything.
It is as if the parent post would not be connected.

Solution:
Try to use cred_submit_complete() instead, see https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete.
This fires a little later than cred_save_data() and all the connected posts are now available.

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
- - 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 3 replies, has 2 voices.

Last updated by Beda 4 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#1489129

Hi there, I've read as much as I can but I can't find an answer to this.

I have TWO posts in a one to many relationship.
VENUES
EVENTS

An event can only be at one venue, but one venue has many events.

What I am trying to do is create a post title for the event based on the fields entered.

I want the post title for an event to be <DATE> at <VENUE TITLE>

I can call the date (albeit in a unix timestamp format) and that works

But when I try and call the title of the parent post (The venue title) it returns nothing

BUT

It works when I EDIT the post. Just not when I'm creating a NEW one

Here is my code

[code]
add_action('cred_save_data','mm_create_title_and_slug',10,2);
function mm_create_title_and_slug($post_id, $form_data) {
if ( ($form_data['id']==1509) | ($form_data['id']==1560)) {
$post = get_post( $post_id );
$date = get_post_meta($post_id, 'wpcf-event-date', true);
$winner = get_post_meta($post_id, 'wpcf-winner', true);
$new_date = strtotime($date);
$relationship = 'venue-event';
$related_post_id = toolset_get_related_post($post_id, $relationship, 'parent' );
$venue_title = get_the_title( $related_post_id );
$title= $date. ' at ' . $venue_title;
$slug = sanitize_title($title);
$args = array('ID' => $post_id, 'post_title' => $title, 'post_name'=>$slug);
wp_update_post($args);
// var_dump($venue_title); die();

}
}
[/code]

#1489193

Related to the code, I refactored this below, please see the comments

add_action('cred_save_data','mm_create_title_and_slug',10,2);
function mm_create_title_and_slug($post_id, $form_data) {
	if ( ($form_data['id']==1509) | ($form_data['id']==1560)) {
		//no need to get_post, the current post ID is already in $post_id delivered by Forms, see DOC and you never seem to use the object
		//$post = get_post( $post_id );

		$date = get_post_meta($post_id, 'wpcf-event-date', true);//OK, this gives you a numeric timestampt
		$winner = get_post_meta($post_id, 'wpcf-winner', true);//OK, not sure what the field is, but it will be a single value too

		//The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. But, you already have a Toolse Types Date field and that is a Timetstamp, not an english text date. So, you do not need this, it is already a timstampt. If you want to convert timestamp to human, use date() instead.
		//$new_date = strtotime($date);

		$relationship = 'venue-event';//OK, if this is your relationship slug

		//Retrieve an ID of a single related post. This will only work to get ONE related post. Is this the goal? Then it's fine. 
		//Please error_log(print_r($related_post_id, true)); so you know what you get here.
		$related_post_id = toolset_get_related_post($post_id, $relationship, 'parent' );

		$venue_title = get_the_title( $related_post_id );//OK, we get the other posts title - as said above, make sure, $related_post_id is what you expect

		$title= $date. ' at ' . $venue_title;//this will now create a string like 328947529387459atTITLE. This because we have a timstamp, that is not converted to human readable format and it concatenates to at and the related post title.

		$slug = sanitize_title($title);//OK, if you want to sanitize 

		$args = array('ID' => $post_id, 'post_title' => $title, 'post_name'=>$slug);
		wp_update_post($args); //here we update the current post with all tha combined.

	}
}

Note, if this does not work, try to hook the entire code to cred_submit_complete
This because the relationship is set late, so it's better to hook in when all operations are done.

----------------------------------------------------------------------------------------------------------------------------------------------------------------

There might be something else that is of interest for you:
The only (now deprecated) official way to display a Parent Post on a Child Form was when not using Many To Many but One To Many or One to One relationships, and it will be possible to display only on the "one" end of the Relationship on the "Many" end.
The ShortCodes are deprecated and not documented but should still work:
[cred-post-parent get='title'] and [cred-post-parent get='url']

Field of parent posts can never be called, and it is generally tricky to call parent data, as long that parent is not connected yet, we do not know the parent

Often, a trick is to read the URL attribute that the "Create child Post Form" creates when you click on a "create child post link"
That allows setting the Post ID of any Shortcode that allows an item attribute.
So for example, you can display the post title using [wpv-post-title] and if you add param ="[wpv-search-term term="parent_page_id"]"

 [wpv-post-title item="[wpv-search-term param='parent_page_id']"] 

Above can be inserted in any Form that creates Child Posts for a Parent and will appear on a page that has a URL parameter of parent_page_id appended.
And that happens always when you insert the Link to that Child Post Form like so in a parent post:

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

Doing so creates a link that links to the page where you insert the "create child form". It takes the ID of the current parent post and passes it to that other page in the URL parameter parent_page_id. This then helps us display anything from the parent post type, inclusive fields, or even queried Views.

I hope this helps, it should avoid some custom code, I think

#1491301

cred_submit_complete is definitely the issue!
I didn't realise it hadn't saved the relationship data even though it had saved the post data

Changing it to this made it work!

#1491559

I'll make sure this gets documented - thanks!

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