• I have two post types I want to list together: News and Events. Events displays an ACF date and News displays the wordpress publish date. I would like an event that occurs in the future to appear above a News item that has already been published. For example:

    EVENT
    June 6th, 2016
    Name of Event

    NEWS
    January 2nd, 2016
    Headline

    This is the code I am using to display this. I just need to modify it to sort by event-start-date if present otherwise default to post date.

    <?php $queryArgs = array(
    			'post_type'      => 'portfolio',
    			'meta_key'		 => 'post_views_count',
    			'orderby'		 => 'meta_value_num',
    			'order'			 => 'DESC',
                'posts_per_page' => $postcount
            );	
    
    		$query = new WP_Query($queryArgs);
    		if( $query->have_posts())
    		{
    			while ($query->have_posts())
    			{
    				$query->the_post();
    				?>
    
    <span class="cat"><?php PrintTerms(get_the_terms( get_the_ID(), 'skill-type' ), ', '); ?></span><br />
    <a href="<?php the_permalink(); ?>">
    <?php if( get_field('event_start_date') ): ?>
    <?php $startDate = DateTime::createFromFormat('Ymd', get_field('event_start_date')); ?>
    <span class="counter"><?php echo $startDate->format('F'); ?> <?php echo $startDate->format('jS'); ?>
    <?php if( get_field('event_end_date') ): ?>
    <?php $endDate = DateTime::createFromFormat('Ymd', get_field('event_end_date')); ?>
    - <?php echo $endDate->format('jS'); ?>, <?php echo $endDate->format('Y'); ?></span>
    <?php else : ?>
    , <?php echo $endDate->format('Y'); ?></span>
    <?php endif; ?>
    <br />
    <?php else : ?>
    <span class="counter"><?php the_date(); ?></span><br />
    <?php endif; ?>
    <span class="title"><?php the_title(); ?></span><br />
    </a>
    </div>
    <?php
    		}
    }
    ?>
  • The topic ‘How to sort posts by two dates’ is closed to new replies.