Skip Navigation

[Resolved] Get value from Select2 Post Relationship Selector to use in cred_save_data $_POST

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

Problem:
I have a Post Relationship (O2M) where a Toolset Post Form creating new M-Posts offers a Parent Post Selector where the user can set the O-Post to which the new post will belong to.
I want to retrieve the value of that Select2 in $_POST and use it in a cred_save_data() to populate other items with it.

How to go about it?

Solution:
A Select2 Parent Post Field in a Toolset Form, assuming a relationship between pages and posts (O2M) can be retrieved with

$_POST["@post-page_parent"]

Note that the HTML input name of the Select2 will have a dot as in

@post-page.parent

But the $_POST will listen to the key with an underscore instead of dot, as in @post-page_parent

So you can get the proper name of the $_POST Select2 input with this process:
- either open the console and find the HTML input name, replace the dot with an underscore and use that
- or, simply var_dump() the $_POST in the Form and see the precise name of all and every $_POST items

This support ticket is created 5 years, 5 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Author
Posts
#1262743
relationship-title.jpg

Tell us what you are trying to do?
I'm trying to make a custom post title based on answers.

The only thing that's not working is the relationship item isn't populating. So either I don't know the proper syntax or I'm thinking about relationships incorrectly.

In this particular case I want the title of the product registration to show the type of product (which is working) AND the model number (post title) for the child item of a related post type. The title of the image attached should be approximately "subwoofer 512d"

An example of the syntax I'm thinking I may have wrong is "wpcf-@product-registration-subwoofer.child" ... but you can see the full function below for context.

Is there any documentation that you are following?
I found very helpful custom title post documentation (thank you), but nothing that references relationships.

FULL FUNCTION FOR CUSTOM TITLE - >

add_action("cred_save_data", "prodreg_save_data_action",10,2);
function prodreg_save_data_action($post_id, $form_data)
{   
    // if a specific form
    if ($form_data["id"]==1048)
    {
  
        if (isset($_POST["wpcf-type-of-product-register"]) && isset($_POST["wpcf-@product-registration-amplifier.child"]) ){
         
            $name = $_POST["wpcf-type-of-product-register"]." ".$_POST["wpcf-@product-registration-amplifier.child"]; 
            // Update the post into the database
            $prodreg_post = array(
                'ID'           => $post_id,
                'post_title' => $name
            );
            wp_update_post( $prodreg_post ); 
        } 
	
		else if (isset($_POST["wpcf-type-of-product-register"]) && isset($_POST["wpcf-@product-registration-subwoofer.child"]) ){
         
            $name = $_POST["wpcf-type-of-product-register"]." ".$_POST["wpcf-@product-registration-subwoofer.child"]; 
            // Update the post into the database
            $prodreg_post = array(
                'ID'           => $post_id,
                'post_title' => $name
            );
            wp_update_post( $prodreg_post ); 
        } 
	
		else if (isset($_POST["wpcf-type-of-product-register"]) && isset($_POST["wpcf-@product-registration-speaker.child"]) ){
         
            $name = $_POST["wpcf-type-of-product-register"]." ".$_POST["wpcf-@product-registration-speaker.child"]; 
            // Update the post into the database
            $prodreg_post = array(
                'ID'           => $post_id,
                'post_title' => $name
            );
            wp_update_post( $prodreg_post ); 
        } 

		else if (isset($_POST["wpcf-type-of-product-register"]) && isset($_POST["wpcf-@product-registration-enclosure.child"]) ){
         
            $name = $_POST["wpcf-type-of-product-register"]." ".$_POST["wpcf-@product-registration-enclosure.child"]; 
            // Update the post into the database
            $prodreg_post = array(
                'ID'           => $post_id,
                'post_title' => $name
            );
            wp_update_post( $prodreg_post ); 
        } 

		else if (isset($_POST["wpcf-type-of-product-register"]) && isset($_POST["wpcf-@product-registration-signal-processor.child"]) ){
         
            $name = $_POST["wpcf-type-of-product-register"]." ".$_POST["wpcf-@product-registration-signal-processor.child"]; 
            // Update the post into the database
            $prodreg_post = array(
                'ID'           => $post_id,
                'post_title' => $name
            );
            wp_update_post( $prodreg_post ); 
        }

		else if (isset($_POST["wpcf-type-of-product-register"]) && isset($_POST["wpcf-@product-registration-install-accessory.child"]) ){
         
            $name = $_POST["wpcf-type-of-product-register"]." ".$_POST["wpcf-@product-registration-install-accessory.child"]; 
            // Update the post into the database
            $prodreg_post = array(
                'ID'           => $post_id,
                'post_title' => $name
            );
            wp_update_post( $prodreg_post ); 
        }

		else if (isset($_POST["wpcf-type-of-product-register"]) && isset($_POST["wpcf-@product-registration-lifestyle-gear-item.child"]) ){
         
            $name = $_POST["wpcf-type-of-product-register"]." ".$_POST["wpcf-@product-registration-lifestyle-gear-item.child"]; 
            // Update the post into the database
            $prodreg_post = array(
                'ID'           => $post_id,
                'post_title' => $name
            );
            wp_update_post( $prodreg_post ); 
        }
		
			else {
         
            $name = $_POST["wpcf-type-of-product-register"]." ".$_POST["wpcf-model-number-product"]; 
            // Update the post into the database
            $prodreg_post = array(
                'ID'           => $post_id,
                'post_title' => $name
            );
            wp_update_post( $prodreg_post ); 
        }

    }
}
#1263029

We cannot assist custom coding at Toolset Support - please see https://toolset.com/toolset-support-policy/.

The code you show above is several lines of non-Toolset API code, which we cannot help with.
I see you use Toolset Fields - but the code itself is not related to Toolset, it's using WordPress API and generic PHP.
As you surely understand our API is just a set of hooks and filters to let you add your own custom code, such as you did above.
We can however not assist code within those functions you hook.

Related to the issue you see, that "relationship item isn't populating", I can give some help:

1. To set or get Relationships with code you can use the Toolset Relationships API shown here https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

2. In your code you do not use that API neither to set or to get relationships, you seem to plan accessing the $_POST of the form.
If you want to get the related posts from the chosen value of the related posts select2 Selector in Toolset Forms, you need to access the valid HTML entities, which are to be obtained from the console, for example.
See hidden link

For example for a Select2 Parent Post Field in a Form it can be $_POST["@post-page_parent"] (Relationships between posts and pages, one to many).
I found that for some reason the Select2's name is replacing the dot with a undesrcore, so you can get the proper name of the input with this process:
- either open the console and find the input name, replace the dot with an udnerscore and use that
- or, simply var_dump() the $_POST in the Form and see the precise name of all and every $_POST item.

I hope that helps!

PS I assume you'll need to use something like $_POST["@product-registration-lifestyle-gear-item_child"] in your case. Notice I removed the prefix and changed dot to underscore.

#1264441

"Notice I removed the prefix and changed dot to underscore" -> Exactly the syntax information I was looking for, got it working. Thank you!

#1264443

My issue is resolved now. Thank you!