• I used a plugin called Advanced Custom Field to create a date picker field. But I can’t figure out how to return the values stored there in my posts. I have a sort that lists all my posts in a custom post type called events. I can only get the title working:

    <h1>Sort events by custom date field</h1>
    <ul>
    <?php 
    
    // args
    $args = array(
    	'post_type'		=> 'events',
    	'posts_per_page'	=> -1,
    	'meta_key'		=> 'speaking_event_date',
    	'orderby'		=> 'meta_value_num',
    	'order'			=> 'DESC'
    );
    
    // query
    $wp_query = new WP_Query( $args );
    
    // loop
    while( $wp_query->have_posts() )
    {
    	$wp_query->the_post();
    
    	echo '<li>' . get_the_title() . '</li>';
    }
    
    ?>
    </ul>

    I was using this thread as a reference:
    http://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker?replies=10

Viewing 1 replies (of 1 total)
  • Thread Starter Squidy McSquid

    (@x1-bot)

    I’m surprised this worked but I figured it out by cobbling together other peoples code from various threads and the api. I also added a way to make the date more readable since the field stores it in ymd format which looks ugly. If you want to tweak it just change the settings in “date_format($date,’d F Y’)” according to php manual

    <?php 
    
    // args
    $args = array(
    	'post_type'		=> 'events',
    	'posts_per_page'	=> -1,
    	'meta_key'		=> 'speaking_event_date',
    	'orderby'		=> 'meta_value_num',
    	'order'			=> 'ASC'
    );
    
    // query
    $wp_query = new WP_Query( $args );
    
    // loop
    while( $wp_query->have_posts() )
    {
    	$wp_query->the_post();
    
    	$date = date_create(''.get_field('speaking_event_date').'');
    
    	echo '<li>' . get_the_title() . ' - ' . date_format($date,'d F Y') . '</li>';
    	echo '<p>' . get_the_content() . '</p>';
    }
    
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘How to list date from an advanced custom field?’ is closed to new replies.