Home › Forums › General Issues › Filter Custom Post Type by ACF Date Picker Field › Reply To: Filter Custom Post Type by ACF Date Picker Field
Okay, I have the posts showing now, in order of date (ascending). But the date is always “January 1 1970”. My save format is the ACF default yymmdd
and my display format is the ACF default: dd/mm/yy
Where would the January 1 1970 be coming from?
<?php get_header(); ?>
<?php
/*
* Create PHP DateTime object from Date Piker Value
* The ACF "Workaround" for PHP 5.2 as shown in the docs
*/
$date = get_field('event_enddate'); // $date = 19881123 (23/11/1988)
// extract Y,M,D
$y = substr($date, 0, 4);
$m = substr($date, 4, 2);
$d = substr($date, 6, 2);
// create UNIX
$time = strtotime("{$d}-{$m}-{$y}");
/* Order Posts based on Date Picker value */
$posts = get_posts(array(
'post_type' => 'event', // name of custom post type
'meta_key' => 'event_enddate', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
if( $posts )
{
foreach( $posts as $post )
{
setup_postdata( $post );
the_title();
echo date('F n Y', $time);
}
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
}
?>
<?php get_footer(); ?>
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.