Skip Navigation

[Resolved] Save post info from CPT1 to CPT2

This support ticket is created 4 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 16 replies, has 2 voices.

Last updated by nicolaS-3 4 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1400091

I have created a ShoppingList CPT (CPT2) that will be linked to Users through Author field, as learned in your knowledge base. When the user is viewing a single Product(offer) post (CPT1) he might decide to add that product to his shopping list. I have created a form (ID=20290) with an "Add to shopping list" button for that and I have also written the following function to save some product info into the shopping list CPT:

add_action('cred_save_data', 'add_to_shoppinglist',10,2);
function add_to_shoppinglist($post_id, $form_data)
{
    //if a specific form
	if ($form_data['id']==20290) //my form ID
   {
       $sl_author = get_current_user_id(); // current user as author
	   $sl_post_title = get_the_title($post_id); // offer title
	   $sl_item_store = get_post_meta($post_id, 'offerta_negozio_nome', true); // store name
	   $sl_item_end_date = get_post_meta($post_id, 'offerta_fine', true); // offer expire date
	   $sl_item_price = get_post_meta($post_id, 'offerta_prezzo_scontato', true); // offer price

       update_post_meta($post_id, $sl_author, $sl_post_title, $sl_item_store, $sl_item_end_date, $sl_item_price);
   }
}

I didn't try it yet because I suppose that the update_post_meta as it is would work on CPT1, but I want to save values to CPT2. Any clue ?
thanks
Regards
Nicola

#1400361

Dear Nicola,

I assume we are talking about this:
You are using Toolset form to create new post of post type "cpt1", after user submit this post form, you want to create another post in post type "cpt2".

If it is, you will need to try another WordPress function wp_insert_post() instead of update_post_meta(), for example this thread:
https://toolset.com/forums/topic/have-a-band-admin-role-to-manage-cpt-band-listing-page/

More help:
https://developer.wordpress.org/reference/functions/wp_insert_post/

#1400625
entries.png

Hi Luo Yang,
following your indications I have changed the function like this:

add_action('cred_save_data', 'add_to_shoppinglist',10,2);
function add_to_shoppinglist($post_id, $form_data)
{
    if ($form_data['id']==20290)
    {
      $my_post = array(
	'post_type' => 'lista-spesa', //this is the CPT2 slug
        'post_title'    => get_the_title($post_id ), // this is supposed to get the CPT1 post title
        'post_status'   => 'publish',
        'post_author' => get_current_user_id(),
		//these 3 lines are supposed to set CTP2 field = CPT1 field
		'lista_offerta_fine' => get_post_meta($post_id, 'offerta_fine', true), 
		'lista_offerta_negozio_nome' => get_post_meta($post_id, 'offerta_fine', true),
		'lista_offerta_prezzo_scontato' => get_post_meta($post_id, 'offerta_prezzo_scontato', true)
      );
          
      // Insert the post into the database
      wp_insert_post( $my_post );
    }
}

and the button works because something is added to CPT2! but what I get are just two strange entries in CPT2 (see picture) and no field has been set other than Author and Status, please advise, thanks

#1401291

For custom fields, I assume you are using Types plugin to setup those custom fields, Types plugin will pre-pend "wpcf-" before field slug, and you can get the field value with function get_post_meta(), and save the field value with update_post_meta(), for example:

...
		// Insert the post into the database
		$new_post_id = wp_insert_post( $my_post );
		// update fields
		$fine = get_post_meta($post_id, 'wpcf-lista_offerta_fine', true);
		update_post_meta($new_post_id, 'wpcf-lista_offerta_fine', $fine);
...

https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/update_post_meta/

#1401813

Hi Luo Yang,
I have modified the function following your instructions but what's strange is that the result is exactly the same, any clue ? thanks

add_action('cred_save_data', 'add_to_shoppinglist',10,2);
function add_to_shoppinglist($post_id, $form_data)
{
    if ($form_data['id']==20290)
    {
			
      $my_post = array(
	    'post_type' => 'lista-spesa',
        'post_title'    => get_the_title($post_id ),
        'post_status'   => 'publish',
        'post_author' => get_current_user_id()
		      );
      $new_post_id = wp_insert_post( $my_post );
	    
		$fine = get_post_meta($post_id, 'wpcf-offerta_fine', true);
		$negozio = get_post_meta($post_id, 'wpcf-offerta_negozio_nome', true);
		$prezzo = get_post_meta($post_id, 'wpcf-offerta_prezzo_scontato', true);

		update_post_meta($new_post_id, 'wpcf-lista_offerta_fine', $fine);
		update_post_meta($new_post_id, 'wpcf-lista_offerta_negozio_nome', $negozio);
		update_post_meta($new_post_id, 'wpcf-lista_offerta_prezzo_scontato', $prezzo);

    }
}
#1402389

Since it is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL and form URL, where I can edit your the PHP codes, I need a live website to test and debug, thanks

#1405041

Thanks for the details, I can log into your website, I assume we are talking about the post form Add listaspesa:

I have done below modification in your website:
Dashboard-> Toolset-> Settings-> Custom code,

Add a snippet: add_to_shoppinglist.php, with below codes:

add_action('cred_save_data', 'add_to_shoppinglist_new',99,2);
function add_to_shoppinglist_new($post_id, $form_data)
{
    //if a specific form
    if ($form_data['id']==20290) //my form ID
   {
       //$sl_author = get_current_user_id(); // current user as author
		$Offerta_id = $form_data['container_id'];
      
		$title = get_the_title($Offerta_id); // offer title
      
      	$my_post = array(
        	'ID' => $post_id, //this is the CPT2 slug
          	'post_title'    => $title, // this is supposed to get the CPT1 post title
        );
      	wp_update_post( $my_post );
      
		$negozio_nome = get_post_meta($Offerta_id, 'wpcf-offerta_negozio_nome', true); // store name
		$fine = get_post_meta($Offerta_id, 'wpcf-offerta_fine', true); // offer expire date
		$prezzo_scontato = get_post_meta($Offerta_id, 'wpcf-offerta_prezzo_scontato', true); // offer price
      
        update_post_meta($post_id, 'wpcf-lista_offerta_fine', $fine);
        update_post_meta($post_id, 'wpcf-lista_offerta_negozio_nome', $negozio_nome);
        update_post_meta($post_id, 'wpcf-lista_offerta_prezzo_scontato', $prezzo_scontato);
 
   }
}

In your case, you can get the offer post ID with this line: $Offerta_id = $form_data['container_id'];

And the FTP access you provided above is not valid, so you will need to remove the PHP codes you mentioned above.

#1405787

Dear Luo Yang,
thank you very much it works perfectly ! but I noticed that every time the button is clicked a new record is added to the shopping list even though it exists already for the same user. Instead of modifying this function I'd prefer to hide the button <div> once the offer has been added to the shopping list, so I need to test if the same record exists in the shopping list before showing the button. I would test if offer_post_title=shoppinglist_post_title AND offer_author=shopping_list author in the view conditional (CO) output, I have tried but CO seems to be working on the same post type only. I was thinking to create a shortcode to be used in CO returning 0 or 1 to test if the record exists already in the shopping list. Am I on the right track ? or can you suggest a better way ? thanks
Regards
Nicola

#1406279
Field-default-value.JPG

Dear Nicola,

For the question:

I would test if offer_post_title=shoppinglist_post_title AND offer_author=shopping_list author in the view conditional (CO) output

I don't think you are on the right track, here are my suggestion:
1) Setup one-to-many relationship between post types "offer" and "shoppinglist"

2) In a single "offer" post, setup a post view:
- Query "shoppinglist_post" posts
- Filter by:
a) Post type relationship between post types "offer" and "shoppinglist"
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/#displaying-many-related-items
b) post's author is same as current logged-in user
https://toolset.com/documentation/user-guides/filtering-views-query-by-author/
- In section View's loop:
a) Within shortcode [wpv-items-found]...[/wpv-items-found], display a message, for example: you have already created shoppinglist post for this "offer" post.
a) Within shortcode [wpv-no-items-found]...[/wpv-no-items-found], display the your post form for creating "shoppinglist" posts(ID 20290)

3) Edit post form for creating "shoppinglist" posts(ID 20290), add the post relationship field into form content using option "Field default value": [wpv-post-id], so it will use current "offer" post ID as default value, see screenshot: Field-default-value.JPG
use some CSS codes to hide the post relationship field, see below document.

More help:
https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/

#1407069
slform.png

Hi Luo Yang,
many thanks for your indications I am learning a lot ...), I followed them carefully and didn't have any issues, although when I show the single post where the form is supposed to appear it doesn't and it happens what shown in the attached picure, could you please check why ?
Content Template -> Template for Offerte
Is it correct that the one-to many relationship doesn't appear in the Admin menu as a M-to-M relationship ?
thanks
regards
Nicola

#1407725

Q1) I show the single post where the form is supposed to appear it doesn't
The problem is in post form:
hidden link
section "Form editor",

I have replaced the codes from:

<div class="hidden">[cred_field field='@offerta-listaspesa.parent' class='form-control' output='bootstrap' select_text='--- not set ---' value='[wpv-post-id]' urlparam='cpt1s cpt2s']<div>[credform]

To:

[credform]
<div class="hidden">[cred_field field='@offerta-listaspesa.parent' class='form-control' output='bootstrap' select_text='--- not set ---' value='[wpv-post-id]' urlparam='cpt1s cpt2s']</div>

All the form fields need be wrapped within [credform] ... [/credform] shortcodes

Q2) Is it correct that the one-to many relationship doesn't appear in the Admin menu as a M-to-M relationship ?
The one-to-many relationship supposed to appear when you edit a offerta post or listaspesa post, for example this Offerta post:
hidden link
In section "Offerte-Listespesa", here you can connect it with other Listespesa posts.

#1408227

Hi Luo Yang,
the form works fine now, thanks, but:
1. the one-to-may relationship isn't actually created, even though the msg says that it was. Editing the post the relationship is empty.
2. the comment box at the end of the page is still not showing. This is the standard WP comment box, styled by Avada. Anything to do with the hidden field in the form ?

Please check, thanks
Regards
Nicola

#1408487

Hi Luo Yang,
CORRECTION re. point 1: the relationship IS created although if you reload the same offer single page the button is shown instead of "This offer is already in your shopping list"

thanks
Regards
Nicola

#1410907

Q1) You haven't complete the step 2) I mentioned above:
https://toolset.com/forums/topic/save-post-info-from-cpt1-to-cpt2/#post-1406279

I have done below modifications in your website:
1) Create a post view "step2":
hidden link

2) Put it into content template:
hidden link
line 34:

<div>[wpv-view name="step2"]</div>

Q2) You can disable the option "Hide comments when displaying this form" in post form "Add listaspesa":
And test again:
hidden link

#1411663
comment.png

Hi Luo Yang,
perfect thanks, now the process works perfectly !
Last issue: when the form/button is displayed the post comment area (WP standard) doesn't show up, while i'ts ok when the message is displayed. I thought the hidden field was guilty but is not .... any clue ? see picture
BTW - I think many people would be interested in creating shopping lists or similar outside WooCommerce, why don't you write a tutorial based on this ?
Many thanks again
Regards
Nicola

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