Hm, for some reason it doesn’t see the first parameter as a datetime.
DateTime::diff() expects parameter 1 to be DateTimeInterface, null given in on line 23.
Now I get the following error:
Uncaught Error: Class ‘WP_Query’ not found
I’ll get the following error.
Call to undefined function get_query_var()
Should I replace the ‘paged’ value with something else?
Oke, so to be sure.
I added an extra line of code, which saves the publication date in an ACF field.
$datetime1 = new DateTime();
$date_created = $order->get_date_created();
$datetime2 = new DateTime($date_created);
$difference = $datetime1->diff($datetime2);
$verschil = "difference " . $difference->days . " days ";
update_post_meta( $post_id, 'datum2', $verschil );
update_post_meta( $post_id, 'datum', $datetime2 );
So the next step is to create a file, which I save in my WordPress directory. The file should be like you showed:
<?php
global $wp_query;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => -1,
'post_type' => 'groeiprocessen',
'fields' => 'ids', //not sure what to add here
'post_status' => array('publish'),
'paged' => $paged
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_id = get_the_ID();
$datetime1 = new DateTime();
$datetime2 = get_field( "datum" );
$difference = $datetime1->diff($datetime2);
$verschil = "difference " . $difference->days . " days ";
update_post_meta( $post_id, 'datum2', $verschil );
endwhile;
endif;
The last step should be to trigger the URL with the Cron Job.
Is that correct? Thanks for your help!
The post_id comes from a custom post type and the posts are all published.
Unfortunately the value seems to not update everyday. Does anyone if I should upgrade the code or add some cron job or something?
$new_post = array(
'post_title' => "Order {$order_id}", // Definieer titel van de post
'post_date' => date('Y-m-d H:i:s'), // Voeg publicatiedatum toe
'post_author' => $user_ID, // Definieer de klant als auteur
'post_type' => 'groeiproces', // Definieer CPT
'post_status' => 'publish', // Publiceer post
);
$post_id = wp_insert_post($new_post);
$now = time();
$date_created = $order->get_date_created();
$dt = new DateTime($date_created);
$date = $dt->format('Y-m-d');
$your_date = strtotime($date);
$datediff = $now - $your_date;
$diff = round($datediff / (60 * 60 * 24));
update_post_meta( $post_id, 'datum', $diff );
Thanks men! Was able to get it working. Really appreciate your help!
So how can I combine that with my current code. Because this is a different field it shouldn’t be part of the repeater field. Could you help me on that?
Could you give me an insight how I can implement this in my code. I am not that advanced in PHP yet.
Thanks John, I was able to upgrade my code using an array eventually.
function create_post_after_order( $order_id ) {
if ( $order_id instanceof WC_Order ){
return;
}
$order = wc_get_order( $order_id );
$order_items = $order->get_items();
foreach ( $order_items as $item_id => $item_data ) {
$product = $item_data->get_product();
$qty.= $item_data->get_quantity();
$name.= $product->get_name();
}
$new_post = array(
'post_title' => "Order {$order_id}",
'post_content' => $content,
'post_status' => 'private',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'groeiproces',
'post_status' => 'publish',
);
$post_id = wp_insert_post($new_post);
// repeater field with multiple subfields
$class_field_key = 'field_61645b866cbd6';
$class_subfield_name = 'field_61645b916cbd7';
$class_subfield_colour = 'field_6165450699ffa';
$class_names = array($name,$name);
$class_colours = array($qty,$qty);
foreach ($class_names as $index => $class_names) {
$class_value[] = array($class_subfield_name => $class_names, $class_subfield_colour => $class_colours[$index]);
update_field( $class_field_key, $class_value, $post_id );
}
}
add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );
Right now the only problem is that this combines the values in one field. e.g.
Name
Product1Product2
Quantity
11
Does anyone know how to split those values and save them separated. e.g.
Product1 1
Product2 1
Thanks in advance!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.