I run a loop for posts which have a custom field with a string like 22/5/2018
and I need to order the posts by date, so I do:
$postOrdered = array();
$queryPosts = new WP_Query(array(
'posts_per_page' => -1,
'post__in' => $postIds,
'order' => 'DESC',
'orderby' => 'DATE',
'meta_key' => 'usp-custom-80',
'type' => 'DATE',
)
);
if( $queryPosts->have_posts() ):
while ( $queryPosts->have_posts() ) : $queryPosts->the_post();
array_push($postOrdered, $post->ID);
$date = usp_get_meta(false, 'usp-custom-80');
echo $date."<br>";
endwhile;
endif;
And I get:
2-6-2015
21-12-2018
26-12-2018
18-12-2018
27-12-2018
12-11-2018
21-12-2018
7-12-2018
5-12-2018
5-12-2018
6-12-2018
19-12-2018
7-12-2018
13-12-2018
24-11-2000
25-11-2018
13-11-2018
I thought of converting the strings to dates by doing
$postOrdered = array();
$date = usp_get_meta(false, 'usp-custom-80');
$date = DateTime::createFromFormat("d.m.Y", $date)->format("m/d/Y");
$queryPosts = new WP_Query(array(
'posts_per_page' => -1,
'post__in' => $postIds,
'order' => 'DESC',
'orderby' => $date,
'meta_key' => 'usp-custom-80',
'type' => 'DATE',
)
);
if( $queryPosts->have_posts() ):
while ( $queryPosts->have_posts() ) : $queryPosts->the_post();
array_push($postOrdered, $post->ID);
$dateOrdered = usp_get_meta(false, 'usp-custom-80');
echo $dateOrdered."<br>";
endwhile;
endif;
But I get nothing