显示一周的帖子

时间:2016-02-04 05:36:08

标签: wordpress custom-fields

有人可以帮助我如何只显示一个星期的帖子吗?我有这个代码在post_date中正常工作:

<?php
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'cat' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    // Using the date_query to filter posts from last week
    'date_query' => array(
      array(
        'after' => '1 week ago'
      )
    )
  ); 
?>
<ul class="weekly-list">
    <?php $the_query = new WP_Query( $args ); ?>
    <?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
    <li>
        <a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
        </a>
    </li>
    <?php } wp_reset_postdata(); ?>
</ul>

但是如何在自定义字段日期中执行此操作。因为我的其他活动是在活动开始前一周发布的。有人能帮助我吗?

感谢。

2 个答案:

答案 0 :(得分:0)

这可能会帮助您获得解决方案:

 $week = date('W');
 $year = date('Y');
 $the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
  if ( $the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?> "><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    <?php else:  ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

答案 1 :(得分:0)

之前的答案是顺利的。只需修改您的date_query

即可
<?php
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'cat' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    // Using the date_query to filter posts from last week
    'date_query' => array(
        array(
            'year' => date( 'Y' ),
            'week' => date( 'W' ),
        ),
    ),
  );
?>
<ul class="weekly-list">
    <?php $the_query = new WP_Query( $args ); ?>
    <?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
    <li>
        <a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
        </a>
    </li>
    <?php } wp_reset_postdata(); ?>
</ul>