Skip Navigation

[Resolved] Update repeatable field's post date as parent post date

This support ticket is created 3 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 3 years, 2 months ago.

Assisted by: Jamal.

Author
Posts
#2231689
2219217-Screenshot_2021_11_12_at_3.57.59_PM.png

Dear Sir/Madam,

I follow the ticket https://toolset.com/forums/topic/update-repeatable-fields-post-date-as-parent-post-date/, I tried

$inv_id = toolset_get_related_post( $post_id, array( 'invoice', 'additional-items' ) );

or

$inv_id = toolset_get_related_post( $post_id, array( 'additional-items' ,  'invoice') );

I got 0 return but not the parent post id.

Here is my full code

function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==101) { // create invoice additional-items product 
        $items = explode("|", $_POST['product-select']);
        $inv_id = toolset_get_related_post( $post_id, array( 'invoice', 'additional-items' ) );
        $inv_date = get_post_meta( $inv_id, 'wpcf-invoice-date', true );
        wp_update_post( array(
            'ID' => $post_id,
            'post_name' => $post_id,
            'post_title' => sprintf("%s (%s)", $items[1], $inv_id),
            'post_date' => $inv_date
        ));
        update_post_meta( $post_id, 'wpcf-item-description', $items[1]);
        update_post_meta( $post_id, 'wpcf-item-amount', $_POST['wpcf-item-quantity'] * $items[2]);
    }
}

Could you directly give me the code how I can get the parent post date if I know the repeatable post id?

Best regards,

Kelvin.

#2231825

Hello and thank you for contacting the Toolset support.

Can you try this:

$inv_id = toolset_get_related_post( $post_id, 'additional-items', 'parent' );

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

I hope this helps. Let me know if you have any questions.

#2232575

Dear Jamal,

No, it return 0.

#2232593

What post type does the form with ID 101 saves? It has to be the RFG otherwise, the post_id would not be coming from the RFG item.

Would you allow me temporary access to check this form further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#2234365

I run a small test on my local test site and I can confirm that this is the signature to get the parent post from an RFG item:

$parent = toolset_get_related_post( $post_id, 'rfg-slug', 'parent' );

I want to test this on your website, but I will need FTP access in order to not crash the website by mistake. Please provide FTP access on your nest reply.

#2235249

Thank you for the FTP access. However, I am afraid, it is not enough to have access to the theme's files. I need some way of debugging the code, and I can't output data because of the redirection that the form performs.

I need access to wp-config.php in order to activate debugging. Then, I need access to the debug.log file in order to check my debugging messages. The file is usually located in /wp-content/ , but some hosting may place it or name it differently. Please update the provided FTP user so it can access the debug.log file.

#2235637

Dear Jamal.

Please try again.

#2237573

Thank you for the credentials. It seems that the action hook is run before Toolset gets the chance to link the RFG item to the parent post. I tested with a higher priority but it did not work.

Then, I tested with the cred_submit_complete hook instead of cred_save_data and it works as expected. The relationship is set and we get the invoice ID using toolset_get_related_post:

add_action('cred_submit_complete', 'my_cred_submit_complete',999999 ,2 );
function my_cred_submit_complete($post_id, $form_data)
{
    
    if ($form_data['id']==101) { // create invoice additional-items product 
        $items = explode("|", $_POST['product-select']);
          $inv_id = toolset_get_related_post( $post_id, 'additional-items', 'parent' );

//				$inv_id = $_POST['@additional-items.parent'];
//				printf("<script>console.log('%s')</script>", print_r($_POST));
//        $inv_date = get_post_meta( '267', 'wpcf-invoice-date', false );
        wp_update_post( array(
            'ID' => $post_id,
            'post_name' => $post_id,
            'post_title' => sprintf("%s (%s-%s)", $items[1], $inv_id, $post_id),
            'post_date' => $inv_date
        ));
        update_post_meta( $post_id, 'wpcf-item-description', $items[1]);
        update_post_meta( $post_id, 'wpcf-item-amount', $_POST['wpcf-item-quantity'] * $items[2]);
    } elseif ($form_data['id']==130) { // create invoice
        wp_update_post( array(
            'ID' => $post_id,
            'post_name' => $post_id,
            'post_title' => sprintf("INV%s-%s",date("Y"), str_pad($post_id, 6, "0", STR_PAD_LEFT)),
        ));
        update_post_meta( $post_id, 'wpcf-invoice-number', $post_id );
        
    }
}

More about the hook here https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

I hope this helps. Let me know if you have any questions.