Support

Account

Home Forums General Issues Sorting events with by Date Picker field

Solved

Sorting events with by Date Picker field

  • I have a custom post type called “events” and a custom date picker field on the posts called “speaking_event_date”. My sort works but I don’t know how to actually display the custom date field for each post! Can anyone help? My php skills are terrible!

    
    <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>
    	

    As you can see I know how to use wordpress built in functions but no clue how to actually show the custom dates in these posts! Thanks

  • All you need to do it something like:

    
    echo '<li>
    ' . get_the_title() . '
    ' .the_field('speaking_event_date') . '
    </li>';
    
  • Thanks! I ended up figuring it out through much trial and error and banging my head against the keyboard. Surprised it worked haha.

    I also added a way to print the date in a more easily readable format. In case anyone else needs the code:

    ` <?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 get_post_meta($post->ID,’speaking_event_date’,true) . ‘<br />’;

    echo ‘<li>’ . get_the_title() . ‘ – ‘ . date_format($date,’d F Y’) . ‘</li>’;
    echo ‘<p>’ . get_the_content() . ‘</p>’;

    }

    ?> `

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

The topic ‘Sorting events with by Date Picker field’ is closed to new replies.