Skip Navigation

[Resolved] Get Parent post ID and populate currently edited post with a Toolset Form

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

Problem:
I want to add data to the title of the currently submitted post with Toolset Forms, the data should come from the parent (related) post of the currently submitted post.
How can I do that?

Solution:
There are 2 options, both need be hooked to a Forms API hook.
You can listen to the Form's inputs in $_POST and get the value from the select2 Field where you use to select the related post in Toolset Forms (if you have this field present).
Then, you can update the Post you are editing with that of that post by simply using the value returned by the $_POST of the Select2 Parent field, which is the ID of the parent post.
With that ID you can get any information about this post using the WordPress API and populate your post as well using the WordPress API.
The select2 for parent selectors in the $_POST will be available with something link `$_POST['@post-type_right-post-type_parent'];`
You can get it's precise value by dumping the $_POST in the Form, to use the precise slug of your relationship selector.
All these actions need to be hooked to cred_save_data(), to fire at the right moment when the Form saves it's data to the database.

The other approach is using the Toolset Relationships API hook toolset_get_related_post() to get the (one) related post to the currently edited one.
This should be done on a cred_submit_complete() Toolset Forms hook to ensure all data, inclusive relationships, are stored in the database already.
Then we can get the related Post ID, and again with that ID, we can get the post data using WordPress API.
From here on the process is the same as with the first approach.

There is a good example here using Toolset Relationships API:
https://toolset.com/forums/topic/get-parent-id-on-new-cred-form-submission/#post-1271597
An example using the $_POST would be the same as above just you have the ID from `$_POST['@post-type_right-post-type_parent'];`. You don't need the Relationships API in this case.

This support ticket is created 5 years, 7 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 4 replies, has 2 voices.

Last updated by Amin 5 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#1269417

I am using the code below to get the ID of the parent post. However, each time I submit the form, the ID is shown as "0" ... which is incorrect.

//Create a dynamic post title by the CRED form. New listing.
add_action('cred_submit_complete','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==5268) {
        $make = get_post_meta($post_id, 'wpcf-listing-make', true);
        $model = get_post_meta($post_id, 'wpcf-listing-model', true);
		$dealer_id = toolset_get_related_post( $post_id, '$dealer-listing', 'parent' );
        $title= $make. ' ' .$model;
		$slugurl= $dealer_id. '-' .$make. '-' .$model. '-' .$post_id;
        $args = array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slugurl);
        wp_update_post($args);
    }
}

How can I get the ID of the parent (Dealer) in the slug of the child (Listing) when I submit a new listing?

Thanks in advance!

#1269741

You mean $post_id or $dealer_id is shown as 0?
You should var_dump() the variables and the data you use to build it so you can see why it's 0.

I think the slug of the toolset_get_related_post is wrong, it cannot be $dealer-listing
Slugs do not permit special signs like $
The example here should help you to get started with the Toolset API for Relationships:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

$writer = toolset_get_related_post( $book_id, array( 'writer', 'book' ) );

As you see we have either an array of post types in the relationship or we pass it as a relationship slug (some-slug). Latter is to use when the relationship is not migrated but new.

Hence, you need to make sure your relationship is not an M2M and make sure you use the right Relationship URL in the code.
Then, if the post of $post_id has a related post, it'll return that ID - otherwise, 0.

Please let me know if with this the issue could be solved on your end!

#1270583

Hi Beda.

I'm sorry, but that didn't help. I am not a programmer, so I don't understand what you explained.

I simply need to place the ID of the parent post (Dealer) into the slug when the cred form is submitted.

Here's the code i'm using. Can you please modify it to fix any errors I may have?

//Create a dynamic post title by the CRED form. New listing.
add_action('cred_submit_complete','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==5268) {
        $make = get_post_meta($post_id, 'wpcf-listing-make', true);
        $model = get_post_meta($post_id, 'wpcf-listing-model', true);
		$dealer_id = toolset_get_related_post( $post_id, 'dealer-listing', 'parent' );
		$title= $make. ' ' .$model;
		$slugurl= $dealer_id. '-' .$make. '-' .$model. '-' .$post_id;
        $args = array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slugurl);
        wp_update_post($args);
    }
}
#1271597

We do not modify or fix the code in the forum so it's ready to use, we explain this here https://toolset.com/toolset-support-policy/

I can help you build the code explaining the Toolset API and usage, as well as inform about generic use of PHP or the WordPress API, as above.

The 2 possibilities are to get the selected parent post ID from the select2 input in the form, or to get it with the Toolset Relationships API as you attempt in the code sample.

To get data when the form saves data and interact with it you can use the cred_save_data hook, to update the Post Title, you will need to apply the WordPress API wp_update_post(), and to get the related post as mentioned you can use the value set in the $_POST or per Toolset relationships API.
In case when working with the Toolset Relationships and API you should hook in when the form completed all operations, so you do it correctly at cred_submit_complete.

An example using cred_submit_complete and the relationships API, which will need to be adapated to your install:

//Hook our custom code to cred_submit_complete 
add_action('cred_submit_complete', 'ts_support_example',10,2);

function ts_support_example($post_id, $form_data)
{
    // if a specific form, change the form ID to which you want this code to apply to
    if ($form_data['id']==5)
    {
        
      //Get the ONE related post to the one we submitted. This works ONLY in a O2M relation
      $related_post = toolset_get_related_post( $post_id, 'page-post', 'parent' );

      //Join whatever values needed
      $title = "String " . $related_post; 

      //Few debug steps     
   		// var_dump($post_id);
   		// var_dump($related_post);
   		
      //Update our post with the WP API
      $my_post = array(
        'ID'           => $post_id,
        'post_title'   => $title,
  	  );
      wp_update_post( $my_post );
      
    }
}

This example works with a Page-Post One To Many Relationship. It will not work in a Many To Many relation or on the end of the "MANY" posts in the relationship

This code is a working example from a existing relationship, which you'd need to edit to match your relationship, and also eventually adapt the code.
If that does not work, I'll likely need a duplicate of the site to debug it locally, I attach below informations about how you can provide this.
https://toolset.com/faq/provide-supporters-copy-site/

Thank you!

#1271759

My issue is resolved now. Thank you!