WordPress.org

Ready to get started?Download WordPress

Forums

Advanced Custom Fields
[resolved] Sorting by date picker (10 posts)

  1. rjpinney
    Member
    Posted 1 year ago #

    I'm using the date picker to set the date for a custom post type (events) and have it set up to order these posts by the custom date field. From the documentation, I have the date output format set to 'yy mm dd' which allows the sort t work properly.

    The problem is that it's difficult for people to read the date in that format on the front end and so I want to re-format after the sort to read, for example, 01 May 2012.

    Alternatively, if there is a way of ordering by date when the date is formatted dd M yyyy then that would be ideal!

    Any help appreciated!

    http://wordpress.org/extend/plugins/advanced-custom-fields/

  2. Stian Andreassen
    Member
    Posted 1 year ago #

    If your date format is yy/mm/dd, you can convert it to a UNIX timestamp with this function:

    function datepicker_to_unix( $date ){
    	$dateparts = explode('/', $date);
    	$unixtimestamp = strtotime(date('d.m.Y', strtotime($dateparts[1].'/'.$dateparts[2].'/'.$dateparts[0])));
    	return $unixtimestamp;
    }

    Use like this:

    $date = get_field('my_date');
    echo date('d F Y', datepicker_to_unix($date ));

    This will display the date like dd M yy; 01 May 2012

  3. zaharamh
    Member
    Posted 1 year ago #

    Just what I needed! Thanks.
    This is a GREAT plugin!

  4. ruhelamin
    Member
    Posted 1 year ago #

    Hi. Im looking to create an upcoming events box in the sidebar but haven't had any luck figuring out how to do this.

    I have a custom post type called events and am using ACF plugin to create a Start Date field, among others.

    I need the typical functionality where the list is sorted by the Start Date field and an event automatically gets off the list when it's gone past the start date of that event.

    Hope someone can help with this. Thanks.

  5. Stian Andreassen
    Member
    Posted 1 year ago #

    Ok, the easiest way is to save your custom field 'startdate' as a unix timestamp. To do this, add the following to your theme's functions.php:

    // CREATE UNIX TIME STAMP FROM DATE PICKER
    function custom_unixtimesamp ( $post_id ) {
        if ( get_post_type( $post_id ) == 'events' ) {
    	$startdate = get_post_meta($post_id, 'startdate', true);
    
    		if($startdate) {
    			$dateparts = explode('/', $startdate);
    			$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
    			update_post_meta($post_id, 'unixstartdate', $newdate1  );
    		}
    	}
    }
    add_action( 'save_post', 'custom_unixtimesamp', 100, 2);

    Then, to get the next five events starting today, use:

    $today = time();
    
    $args = array(
    	'post_type' => 'events',
    	'posts_per_page' => 5,
    
    	'meta_query' => array(
    		array(
    			'key' => 'unixstartdate',
    			'compare' => '>=',
    			'value' => $today,
    		)
    	),
    
    	'meta_key' => 'startdate',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    );
    
    $query = new WP_Query( $args );
    $events = $query->posts;
  6. ruhelamin
    Member
    Posted 1 year ago #

    Thanks stiand. I couldn't get this code to work... maybe i was doing something wrong. But thanks to the info from the code snippets you gave, i found similar solutions, which eventually after tweaking and testing, i've managed to get it working now.

    Thanks very much for the help.

  7. ssalgado
    Member
    Posted 1 year ago #

    Thanks stiand, I am having trouble converting the timestamp from 'Ymd' to 'd.m.Y'

    The function you wrote seems to work but it pulls the wrong value. All my events are pulling the date "January 01, 1970"

    Any idea what I could be doing wrong?

  8. wearepapertiger
    Member
    Posted 1 year ago #

    ACF will only let you sort by date s long as you use the following default date format: yy_mm_dd (as mentioned here: http://www.advancedcustomfields.com/docs/field-types/date-picker/ )

    you would need to alter stiand's code for

    functions.php
    to the following:

    function datepicker_to_unix( $date ){
    	$dateparts = explode('_', $date);
    	$unixtimestamp = strtotime(date('d.m.Y', strtotime($dateparts[1].'/'.$dateparts[2].'/'.$dateparts[0])));
    	return $unixtimestamp;
    }

    Where $dateparts = explode('/', $date); becomes $dateparts = explode('_', $date);

    That did the trick for me. Then you can use stiands following code and it will work:

    $date = get_field('my_date');
    echo date('d F Y', datepicker_to_unix($date ));
  9. Corneliatt
    Member
    Posted 1 year ago #

    Ok, thanks for your help. I've solved it!

  10. mjrwebdesign
    Member
    Posted 1 year ago #

    This post has been really helpful and although it has been solved I wanted to add that the below is working for me without the need for the datepicker function

    $date = date_create(''.get_field('driver_start_date').'');
    echo date_format($date,'d F Y');

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic