Display post for a week

Can someone help me how to display the post only for a week? i have this code that works fine in 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>

but how to do it in the custom field date. Because my other events is posted a week before the events. Can someone help me?

Read More

Thanks.

Related posts

2 comments

  1. This Might help you to get solution:

     $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; ?>
    
  2. The previous answer was along the good track. Just modify your 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>
    

Comments are closed.