donglian6625 2015-07-23 17:57
浏览 150

Wordpress查询和按日期排序

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

  • 写回答

1条回答 默认 最新

  • duanli9930 2015-07-24 06:11
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看