0

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

rob.m
  • 9,843
  • 19
  • 73
  • 162
  • The idea of converting to date it's ok. The problem is that you cannot 'order by' something outside the loop. Covert the string inside the database to a date time database field first. – gtamborero Dec 09 '18 at 22:38
  • @gtamborero yeah but that means that each time I will create a new post I will need to manually convert the field to a date? – rob.m Dec 09 '18 at 22:39
  • You can achieve it automatically coding or using a custom fields plugin as "ACF" or similar. – gtamborero Dec 09 '18 at 22:43
  • any example of how to change the field to date? – rob.m Dec 09 '18 at 22:43
  • this maybe can solve your problem if you need also a date time picker inside your posts: https://www.advancedcustomfields.com/resources/date-time-picker/ – gtamborero Dec 09 '18 at 22:46
  • @gtamborero i know I could use a date filed but unfortunately the field isn't a date – rob.m Dec 09 '18 at 22:47

1 Answers1

0

Change your code to look something like following:

if( $queryPosts->have_posts() ):
    $dateOrdered = [];
    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;

function custom_sort_dt($a, $b) {
    return strtotime($a) - strtotime($b);
}
usort($dateOrdered, "custom_sort_dt");
print_r($dateOrdered);

Please refer:

Post

oreopot
  • 3,392
  • 2
  • 19
  • 28
  • yeah but that is ordering an array but how can I get the post of each of the ordered date now? – rob.m Dec 09 '18 at 22:43
  • maybe we should create a multi dimensional array where we associate both the date and the ID, then order the array by date and the IDs will be ordered to based on their dates. Then I can print each post ID and will be ordered? Not sure – rob.m Dec 09 '18 at 22:46
  • can you show the structure of the output array of query. – oreopot Dec 09 '18 at 23:31
  • I created a new question which is getting me closer, used your code too https://stackoverflow.com/q/53697794/1018804 – rob.m Dec 09 '18 at 23:37