简体   繁体   English

按时间和日期获取wordpress帖子

[英]Get wordpress posts by time and date

I'm trying to get wordpress post by date and time, for example from 02/01/2014 to 02/05/2014 and time 17:10, somebody knows? 我想按日期和时间获取wordpress帖子,例如从02/01/2014到02/05/2014和17:10时间,有人知道吗?

thanks for the help 谢谢您的帮助

The WP_Query class can fulfill this. WP_Query类可以满足此要求。 Just set up a new query, passing in the appropriate arguments: 只需设置一个新查询,并传入适当的参数即可:

$args = array(
    'date_query' => array(
         // limit to between these dates
        array(
            'after'     => '2014-02-01',
            'before'    => '2014-02-05', //remove this line if no upper limit
            'inclusive' => true,
            ),
         // limit to posts before 17:10 (not tested)
        array(
            'hour'      => 17,
            'minute'    => 10,
            'compare'   => '<=',
            ),
         // limit to posts after 08:30
        array(
            'hour'      => 08,
            'minute'    => 30,
            'compare'   => '>=',
            ),
        ),
    'posts_per_page' => -1,
    );
$my_date_query = new WP_Query( $args );

Then run your loop using the query object you just instatiated: 然后使用刚刚设置的查询对象运行循环:

<?php if ( $my_date_query->have_posts() ) : ?>

  <!-- the loop -->
  <?php while ( $my_date_query->have_posts() ) : $my_date_query->the_post(); ?>
    <h2><?php the_title(); ?></h2>
  <?php endwhile; ?>
  <!-- end of the loop -->

  <?php wp_reset_postdata(); ?>

<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

The date parameters section gives a good overview of the types of post queries you can create: http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters 日期参数部分很好地概述了您可以创建的后期查询的类型: http : //codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

NB Don't use query_posts . 注意 :不要使用query_posts From the codex : 法典

query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. query_posts()是通过将页面的主查询替换为新的查询实例来修改页面的主查询的过于简单和有问题的方式。 It is inefficient (re-runs SQL queries) and will outright fail in some circumstances 它效率低下(重新运行SQL查询),在某些情况下将完全失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM