Dear Sir/Madam,
I understand the repeatable field is saved as like as a child post in database, how can I update the repeatable filed's post date same as the parent post date?
I have a custom post type invoice which contain repeatable field "product", I will update the repeatable field when save as below
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==101) {
$items = explode("|", $_POST['product-select']);
wp_update_post( array(
'ID' => $post_id,
'post_name' => $post_id,
'post_title' => sprintf("%s - %s", $items[2], $items[1]),
'post_date' => ( same as parent invoice date )
));
update_post_meta( $post_id, 'wpcf-item-description', $items[1]);
update_post_meta( $post_id, 'wpcf-item-sku', $items[2]);
update_post_meta( $post_id, 'wpcf-item-amount', $_POST['wpcf-item-quantity'] * $items[3]);
}
}
How can I get the invoice date when I update the post_date?
Best regards,
Kelvin.
Hello,
Please elaborate the questions with more details:
When do you want to update the repeatable field's post?
After save the " invoice" post? or after save the "product" item?
Dear Luo Yang,
I want to save the "product" item but copy the post date of the invoice.
Best regards,
Kelvin.
Dear Luo Yang,
I refer to the sampel code
$writer = toolset_get_related_post( $book_id, array( 'writer', 'book' ) );
What is the $relationship between the invoice and product? Refer to my screenshot, additional-item is the product.
After I can update the date of the product record same as the invoice month, how can I query all products that belong to the same date, the query may cross around several invoices
For example
Invoice 001 (Date 1-May-2021)
Product A x 3
Product B x 6
Product C x 2
Invoice 002 (Date 5-May-2021)
Product A x 7
Product C x 3
I want to make a View to list all products make in the invoice in May, the result should be
Sales in May 2021
Product A = 10
Product B = 6
Product C = 5
How can I make this view report?
I assume you are going to do it in your custom PHP codes, you can use WP built-in class WP_Query to query products posts, and filter the result by your custom date field, and get the all products that belong to the same date:
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
Two questions:
1. Can do it with View?
2. Before your answer, sorry I did edit the reply. I refer to the sample code
$writer = toolset_get_related_post( $book_id, array( 'writer', 'book' ) );
I try
$inv_id = toolset_get_related_post( $post_id, array( 'invoice', 'additional-items' ) );
but I get the inv_id is 0
What is the $relationship between the invoice and product? Refer to my screenshot, additional-item is the product.
The relationship slug is same as your repeatable field group slug: additional-items