Skip Navigation

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

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 hace 5 años, 5 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 3 respuestas, tiene 2 mensajes.

Última actualización por shawnW-3 hace 5 años, 5 meses.

Asistido por: Beda.

Autor
Mensajes
#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 enlace oculto

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!