There are quite a number of ways to achieve this and to optimize this to make it efficient as well.
Here is just a basic idea to achieve this by using the offset
parameter in WP_Query
and using the php function date()
to get the current week number.
First, you should get the current week number with date( 'W' )
. You can decide to take this as your starting point, which will be 15
this week. You can also decide to start this at 1 by deducting 15 minus 1 from the current week. As from 1st January you will need to recalculate this differently to get the correct offset. You can go and play around with this to match your needs
Next, calculate the offset
. The offset will be the current week minus 1 to make sure that your first offset is 0
to get the first post and 51
to get the 52nd post (last post)
Here is a sample query to start with this week at 15
; (This is just the basics, remember to reset your query with wp_reset_postdata()
)
$args = array(
'post_type' => 'news',
'posts_per_page' => 1,
'no_found_rows' => true,
'offset' => (date( 'W' ) - 1)
);
$q = new WP_Query( $args );
var_dump( $q->posts );
This should give you one post from the post type news
which will change once a week with the start of the new week