Support

Account

Home Forums General Issues Filter Custom Post Type by ACF Date Picker Field

Solved

Filter Custom Post Type by ACF Date Picker Field

  • Love this plugin! I will not use WordPress without it. So thank you.

    I’m working on a project and just wanted to post a question before I spend too much more time digging further (I haven’t been able to find the information I need so far).

    I’m using the the Date Picker Field Type for a “Start Date” and an “End Date” attached to an “Events” Custom Post Type. I now need to filter my Event Posts by the “End Date” – if the Event is over it needs to show on the Past Events page and if the Event is still upcoming it needs to show on the Future Events page..and this is based on the date selected in the ACF field.

    Is this possible? If so, I understand you cannot guide me through the whole coding process, but a push in the right direction would be very helpful.

  • Hi @codeview

    I think the best thing to do is jump on google and search for:
    acf date picker event

    There are many many results as this is a regularly discussed topic. I’m a but perplexed as to why you didn’t find any relevant information in your serach, but I can confirm that there are lots of topics to read about.

    Thanks
    E

  • I had searched the forums for “filter by date picker” and only found this post: (http://support.advancedcustomfields.com/forums/topic/filtering-cpt-with-date-picker-custom-field/) that seemed like it might be somewhat helpful at first but I cannot make sense of it.

    I did try the example code on the Date Picker Field Documentation page, as a starting point, but I was getting this error on the events page: Fatal error: Call to undefined method DateTime::createfromformat()

    After much hair pulling I see that I mistakenly thought I was running PHP 5.3.24 (on my hosting) but today remembered this particular site is on different hosting (the client’s hosting) and their server is running PHP 5.2.6.

    So now I’ve attempted this code (from the Date Picker docs page, referring to PHP versions less than 2.3) and all it outputs is “01/01/1970January 1 1970”. 🙁

    <?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_end-date'); // $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}");
     
    // format date (23/11/1988)
    echo date('d/m/Y', $time);
     
    // format date (November 11th 1988)
    echo date('F n Y', $time);
     
    /* Order Posts based on Date Picker value */
    $posts = get_posts(array(
    	'post_type' => 'event', // name of custom post type
    	'meta_key' => 'event_end-date', // name of custom field
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC'
    ));
     
    if( $posts )
    {
    	foreach( $posts as $post )
    	{
    		setup_postdata( $post );
     
    		echo 'hello?';
    
    	}
    	wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    }
      
    ?>
    <?php get_footer(); ?>
    
  • 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(); ?>
  • Hi @codeview

    Thanks for the follow up.

    You will need to debug your code line by line to find out why the date is incorrect. You can debug like this:

    
    <?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)
    
    echo '<pre>';
    	print_r( $date );
    echo '</pre>';
    die;
    
    // extract Y,M,D
    $y = substr($date, 0, 4);
    $m = substr($date, 4, 2);
    $d = substr($date, 6, 2);
    
    echo '<pre>';
    	print_r( $y );
    echo '</pre>';
    echo '<pre>';
    	print_r( $m );
    echo '</pre>';
    echo '<pre>';
    	print_r( $d );
    echo '</pre>';
    die;
    
    // create UNIX
    $time = strtotime("{$d}-{$m}-{$y}");
    
    echo '<pre>';
    	print_r( $time );
    echo '</pre>';
    die;
    
    ?>
    

    Please note there are 3 sections to this code which print out and die. Once you are happy with the result, just remove that print_r section and allow the next to run.

    This should help explain where the issue starts

    Thanks
    E

  • Thanks Elliot. When I paste that code the page only outputs <pre> </pre>, nothing else. So I would assume that means the code dies at the first part, so there is a problem with the line: $date = get_field('event_enddate');

    But I don’t know what 🙁 I can use get_field(‘event_enddate’) fine on any other page. Any ideas?

  • Hi @codeview

    What do you mean by “it works fine on any other page”?

    Does this page use a custom template? Or perhaps it doesn’t contain a date value?
    What is different about this page to your other pages?

    Thanks
    E

  • Ugh! I didn’t see that you removed the post type query bits when you provided the debug code, so the proper post type was not being queried:

    	/* 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'
    	));

    But I still only get <pre> </pre> output.

    I give up. 6+ days on & off trying to figure out what seems like it should be a fairly basic feature. This project needs to move forward 🙁

    I just want the date output as “Month Day, Year” and a Past Events page to show only events with an end date that has passed and an Upcoming Events page to show only events with a future date.

  • P.S. – I purchased the Repeater add-on today. Very nice. It will be very helpful. Was hoping to only need the ACF Plugin for this project but looks like something else/another plugin will need to be implemented for the Events section.

  • Hi @codeview

    6 days is a long time to sit on 1 issue. Even though this issue still remains, I hope that in the future you can see how any code related issue snowballs from 1 line of code:

    $date = get_field('event_enddate');

    I would advise you to debug your code line by line to find out at which line the data is incorrect.

    Hope that helps.

    Thanks
    E

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Filter Custom Post Type by ACF Date Picker Field’ is closed to new replies.