I'm trying to create a list of 10 products with a publication date in the future ordered most recent to most in the future. The publication date is stored as longtext in an Advanced Custom Field called "publication_daet" in Ymd format. The code I'm using is below but the results are not as expected (results are below the code block). Any ideas?
<section id="recent">
<ul class="row-fluid">
<?php
$today = date('m/d/Y');
$args = array(
'post_type' => 'product',
'showposts' => 10,
'meta_key' => 'publication_date',
'meta_value' => $today,
'meta_compare' => '>',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="span3">
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<b><?php $date = DateTime::createFromFormat('Ymd', get_field('publication_date'));?><?php echo $date->format('m/d/Y'); ?></b>: <?php the_title(); ?>
</a>
</li><!-- /span3 -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!-- /row-fluid -->
</section><!-- /recent -->
Results:
11/20/2013: The Capitals
09/03/2015: Orphan Black Card Game
07/10/2014: Sons of Anarchy Men of Mayhem
07/25/2013: Unicorno Frenzies
06/25/2015: Medieval Academy
07/03/2014: Krosmaster Arena DuelPack
01/29/2015: Redacted
06/15/2010: 3:16 Carnage Amongst the Stars
04/29/2015: BattleTech Recon Lance Pack
12/17/2014: Duke Siege Engines Middle Ages
Your date format in your custom field is correct. Your date format needs to be unix time stamp or in the format of Y-m-d
.
However, the format of today's date is incorrect. Unlike the date_query
field, custom fields compare literally, no convertions are done to any value, so your formats of values to be compared must match. In short, if your date is stored in a custom field as unix timestamp, your date to be compared must be unix timestamp, otherwise your comparisons fail. The same with date formats, if your date is saved as Ymd
, your format to compare must be Ymd
. If your format is Y-m-d
, your format to compare must be Y-m-d
. If these format don't match, your comparison is doomed
To solve your issue, change the value of $today
to match the same format as your custom field