I have a custom post type with a custom field that takes a date. The posts are later ordered by the date in that custom field. Unfortunately I formatted the date incorrectly and there are already a number of posts with the post meta value as dd-mm-yyyy. I have found that to use this value as orderby (WPQuery) it needs to be yyyy-mm-dd. So I was going to try:
$args = array(
'posts_per_page' => -1,
'post_type'=> 'match_report'
);
$wp_query = new WP_Query();
$wp_query->query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$matchdate = get_post_meta($post->ID, 'report_date', true);
$new_matchdate = strftime("%Y-%m-%d", $matchdate);
update_post_meta($post->ID, 'report_date', $new_matchdate);
endwhile;
wp_reset_postdata();
endif; ?>
but I am getting
Notice: A non well formed numeric value encountered in...
and when I echo $new_matchdate
I get 1970-01_01 so obviously I am doing this incorrectly. Any help would be appreciated.
I have a custom post type with a custom field that takes a date. The posts are later ordered by the date in that custom field. Unfortunately I formatted the date incorrectly and there are already a number of posts with the post meta value as dd-mm-yyyy. I have found that to use this value as orderby (WPQuery) it needs to be yyyy-mm-dd. So I was going to try:
$args = array(
'posts_per_page' => -1,
'post_type'=> 'match_report'
);
$wp_query = new WP_Query();
$wp_query->query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$matchdate = get_post_meta($post->ID, 'report_date', true);
$new_matchdate = strftime("%Y-%m-%d", $matchdate);
update_post_meta($post->ID, 'report_date', $new_matchdate);
endwhile;
wp_reset_postdata();
endif; ?>
but I am getting
Notice: A non well formed numeric value encountered in...
and when I echo $new_matchdate
I get 1970-01_01 so obviously I am doing this incorrectly. Any help would be appreciated.
Oh my. You're doing some freaky stuff there. :)
Please try it like this:
$args = array(
'posts_per_page' => -1,
'post_type'=> 'match_report',
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$matchdate = get_post_meta(get_the_ID(), 'report_date', true);
$new_matchdate = DateTime::createFromFormat('d-m-Y', $matchdate);
update_post_meta(get_the_ID(), 'report_date', $new_matchdate->format('Y-m-d'));
}
wp_reset_postdata();
}
// EDIT
If you have problems doing it the object oriented way, try it procedural like so:
$args = array(
'posts_per_page' => -1,
'post_type'=> 'match_report',
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$matchdate = get_post_meta(get_the_ID(), 'report_date', true);
$new_matchdate = date_create_from_format('d-m-Y', $matchdate);
update_post_meta(get_the_ID(), 'report_date', date_format($new_matchdate, 'Y-m-d'));
}
wp_reset_postdata();
}