1

I have custom post type named 'NEWS' where i already published 52 posts at a time. Now, in front i would like to display them ONE NEWS for ONE WEEK. So 52 NEWS will cover one year for me.

My Question, how can i run the query & schedule to display 1 News for 1 Week. There has no mandatory issue that for certain NEWS for certain WEEK. Simply display as a asceding/descending way for each week.

Really appreciate if anyone can help to get rid of this issue.

Thanks in advance.

1
  • What work have you done so far? Please post your code. Commented Apr 6, 2015 at 9:30

1 Answer 1

1

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

2
  • Excellent !!!! Really great effort. I was really awaiting for this type of query. You are awesome @Pieter Goosen. I believe this can solve my problem :) Thanks a million. Commented Apr 6, 2015 at 9:44
  • My pleasure. Enjoy :-) Commented Apr 6, 2015 at 9:45

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.