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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示