0

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. Unfortuately 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.

1

1 Answer 1

2

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();
}
3
  • Ha ha, yes, It appears so ;) I'm getting: Catchable fatal error: Object of class DateTime could not be converted to string in..
    – mantis
    Commented Jan 29, 2014 at 10:51
  • Please see (and try out) my edit. If you still get any errors, this might be due to invalid post meta information.
    – tfrommen
    Commented Jan 29, 2014 at 10:56
  • It was because I had erased them all in one of my attempts :( All good now :). I'm curious why I should use get_the_ID()rather than post->ID? Many thanks!
    – mantis
    Commented Jan 29, 2014 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.