Sort by a custom date field I've got this code to work, but being really new to PHP, I don't know if this is proper. I've sorted a CPT loop by a custom date.


I'm displaying items from most recent (or future date) to oldest
if the custom date is in the future I display an additional HTML element that says Coming Soon. I've used strtotime for the custom date and compared it to today.


I'm most interested in knowing a better method for converting and comparing time, and if this loop is a proper method of using orderby with a custom date.

get_field() is from the Advanced Custom Fields WordPress plugin.

&lt;?php

    //Loop arguments

    $today = date('Ymd');
    $args = array(
                'post_type' =&gt; 'release',
                'showposts' =&gt; 6,
                'meta_key'  =&gt; 'release_date',
                'orderby'   =&gt; 'meta_value_num', // orderby meta field date
                'order'     =&gt; 'DESC'
            );
    // query and loop
    $release_query = new WP_Query( $args );

    // $release_date = get_field('release_date');

    while ( $release_query-&gt;have_posts() ) : $release_query-&gt;the_post();
    // don't duplicate featured posts
    if ( in_array($post-&gt;ID, $do_not_duplicate) ) continue;
        update_post_caches( $posts );
?&gt;
    &lt;li class="grid large-4 medium-6 small-12 columns"&gt;
    &lt;div class="cd"
        &lt;?php 

            $image = get_field('cd_cover');

            if( !empty($image) ): ?&gt;

                &lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php the_title_attribute(); ?&gt;"&gt;&lt;img src="&lt;?php echo $image['url']; ?&gt;" alt="&lt;?php echo $image['alt']; ?&gt;" /&gt;&lt;/a&gt;

            &lt;?php endif; ?&gt;
        &lt;h2&gt;
            &lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php the_title_attribute(); ?&gt;"&gt;
                &lt;?php the_title(); ?&gt;
            &lt;/a&gt;
        &lt;/h2&gt;
        &lt;?php 
        $cd_date = get_field('release_date');
        $release_date = date('Ymd', strtotime($cd_date)); // convert custom date to something manageable?
        if ( $release_date &gt; $today ) {
            echo '&lt;p class="soon"&gt;&lt;span&gt;Coming Soon!&lt;/span&gt;&lt;/p&gt;';
        } 

        ?&gt;
        &lt;/div&gt;&lt;!-- /cd --&gt;
    &lt;/li&gt;

    &lt;?php 
        //release loop ends
        endwhile;
        // reset
        wp_reset_postdata();
    ?&gt;
I do know nothing about WordPress, so I will not be able to answer if the loop is a proper method for using order by. 

Converting and comparing time

The DateTime class has great utilities for exactly this kind of thing. You have DateTime::createFromFormat to use your custom format, DateTime-&gt;format for formatting, and DateTime-&gt;sub and DateTime-&gt;diff for comparing.

Timestamp

For very simple comparing like "is this date bigger than that date," you could use timestamps (AKA time()) and compare them with &lt;&gt; as a timestamp is just a big number (seconds from 1970 until now). This will be more reliable than comparing date strings.