1

I'm trying to display last posts with WordPress date, not date from server.

<?php 
$week = date( 'W' );
$year = date( 'Y' );
$query = new WP_Query( 'year=' . $year . '&w=' . $week );

while ( $query->have_posts() ) { $query->the_post();

    // Page title
    the_title(); 


} // end while

// Restore original Post Data
wp_reset_postdata();

// Save memory
unset( $query );

;?>

what is wrong? How i can use to show last x day posts with date wordpress default?

2
  • what ddo you mean by last x day posts. do you mean yesterdays post
    – terminator
    Aug 31, 2015 at 22:15
  • I mean e.g. last 7day with date from wordpress not from server.
    – Horby
    Aug 31, 2015 at 22:16

1 Answer 1

3

you need to study about date query. Anyways following should work

<?php 
$args = array(
  'posts_per_page' => -1,  
  'date_query' => array(
     array(
           'after' => '7 days ago'
           )
     )
  );

$query = new WP_Query($args);

while ( $query->have_posts() ) { $query->the_post();

    // Page title
    the_title(); 


} // end while

// Restore original Post Data
wp_reset_postdata();

// Save memory
unset( $query );

;
?>
4
  • thank you, but i want show by date not by time. e.g. all posts last 7 day by sort by date not by time hour.
    – Horby
    Aug 31, 2015 at 22:24
  • please see the edited code
    – terminator
    Aug 31, 2015 at 22:26
  • If i set this: 'after' => '1 days ago' i have 5 posts 1 in 2015-09-01 and 4 in 2015-08-31, this only show 4 posts, from 1 in 2015-09-01 and 3 in 2015-08-31. I want to show all posts on the date specified, regardless of the time.
    – Horby
    Aug 31, 2015 at 22:51
  • you 'll have to use 'posts_per_page' => -1 as arguments for WP_Query to display all the posts and ignore the pagination. Please see the edited code
    – terminator
    Sep 1, 2015 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.