Skip Navigation

[Closed] Passing The ID of Related Post To Another Custom Field

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
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: Africa/Casablanca (GMT+01:00)

This topic contains 7 replies, has 2 voices.

Last updated by Jamal 4 years, 2 months ago.

Assisted by: Jamal.

Author
Posts
#1779409

I'm having trouble passing the ID of a related post to another custom field using CRED SAVE.

The value getting pushed every time is '0', so i'm assuming the $stream_product_id line below isn't setup correctly.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==1386)
{
$product_id = get_post( get_the_ID() );
$stream_product_id = toolset_get_related_post( $product_id, 'product-stream', 'parent' );
update_post_meta($post_id, 'wc_pay_per_post_product_ids', $stream_product_id, true);
}
}

Thanks so much for your help in advance!

#1780241

Hello and thank you for contacting the Toolset support.

I think that get_the_ID() is not returning the correct post ID. It may return the page ID, if the form is used on a page.

I'll suggest using the $post_id that Toolset passes to your function in the first argument:

$product_id = get_post( $post_id );
$stream_product_id = toolset_get_related_post( $product_id, 'product-stream', 'parent' );

Or just:

$stream_product_id = toolset_get_related_post( $post_id, 'product-stream', 'parent' );

I hope this helps. Otherwise, I'll need to take a closer look at your site and check that closely.

#1780255

Unfortunately that didn't work. It's still passing the value '0' to the custom field.

Maybe it's the process that isn't working for what i wanna do. Here's a run-down:

1) CRED Form that creates a Woo Product
goes to another form->
2) CRED Form that creates a Stream (custom post type) and has a field that establishes relationship to Woo Product
3) CRED Save Data should get the post ID of the related Woo Product, then push that value to a custom field of Woo Product named "wc_pay_per_post_product_ids"

For the record, i've replaced the value "wc_pay_per_post_product_ids" in the update_post_meta portion of CRED SAVE DATA with a generic number to see if that worked, and it did. So i'm not sure what the problem is.

Keith

#1780333

Please note that $post_id is the ID of the post that is being created or edited by the form. So it will be the ID of the Stream post.
You will need to use it in toolset_get_related_post to get the product_id.

And, I understood, that you want to update the post_meta of the product, rather than the Stream post, right?
In that case, "update_post_meta" should use the product_id, instead of the Stream post id.

If you still find trouble implementing this, please allow me temporary access to your website to check this further. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
I will need the following details:
- What form is used to create the product, and on what page it is being used?
- What form is used to create the Stream post, and on what page it is being used?
- Where is located your custom code?
- FTP access to be able to recover the website, in case, I was updating the custom code and I crashed the site.

#1780393

Thank you for the credentials. I confirm they are working to me.

Here what I gathered so far:
- What form is used to create the product, and on what page it is being used?
The form is "Create Stream Product" and used in the page "Add New Stream".

- What form is used to create the Stream post, and on what page it is being used?
The form is "Create Stream Page" and is used in the page "Add New Stream – Step 2"

I will still need to know:
- Where is located your custom code? It is not available in Toolset->Settings->Custom code. And I do not have access to the theme editor.
- FTP access to be able to recover the website, in case, I was updating the custom code and I crashed the site.

#1780409

Thank you for the FTP access. As I will need to understand better the workflow of your process and where I can check the value of the custom field(probably in the database, using a tool, such as phpMyAdmin or Adminer), I wonder if you could test the following code:

function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==1386)
    {
        // $product_id = get_post( get_the_ID() );
        $stream_product_id = toolset_get_related_post( $post_id, 'product-stream', 'parent' );
        update_post_meta($post_id, 'wc_pay_per_post_product_ids', $stream_product_id, true);
	}
}
#1780413

That didn't work. It actually didn't push a value at all.

#1781387

It would be hard for me to debug the code on your current website. For that reason, I created a test site on our platform, please reproduce a minimal example that can be similar to your case and let me debug it.
You can log in to our test site with the following URL hidden link

The topic ‘[Closed] Passing The ID of Related Post To Another Custom Field’ is closed to new replies.