I am working on my WordPress blog and in the footer area I have a slider that consists of 4 posts. That slider is called "You may have missed" and currently it is showing the latest posts. With 'rand' arg I managed to sort that it shows random posts but my concern is that it can go months backward. I want to exclude current week posts and only show the week before posts so I can have a valid you "You may have missed" in my blog. For example, if current week is 34, show posts only from week 33, if it is week 45 show posts only from week 44 etc. I want to solve it through arrays.
This is what I currently have
$footer_post_type = array(
'posts_per_page' => 4,
'post__not_in' => get_option('sticky_posts'),
'year' => date( 'Y' ),
'week' => strftime( '%U' ),
'orderby' => 'rand',
'post_type' => array(
'post'
),
CodePudding user response:
Should be pretty straightforward. Just have to subtract 1 from your current week.
You can shorten this code a bit but included everything for demo purposes:
$week = date( 'W' ); // Get current week of the year
$year = date( 'Y' ); // Get current year
$last_week = $week - 1; // Subtract 1 to get previous week number
// Create your query args
$args = array(
'posts_per_page' => 4,
'post__not_in' => get_option('sticky_posts'),
'date_query' => array(
array(
'year' => $year, // Year here
'week' => $last_week, // Week here
),
),
'orderby' => 'rand',
'post_type' => 'post'
);
$query = new WP_Query( $args );