I would like to "Drip" lessons for my class - 1 a day. In the index for the lessons, on the first day, you would see the first lesson, and then on the second day, the second and first lesson, etc. - with today's lesson at the top (shown in the diagram).
I'm checking the current user - and finding out what 'team' they are on. that team has a start_date
- and so I can compare the start_date with today's date and get the difference. That's going well.
<?php $args = array(
'post_type' => 'lesson',
'orderby' => 'post_date', // or date?
'order'=> 'ASC',
'posts_per_page' => daysAvailable(), // difference between today/start date?
// if there are 100+ lessons... and I only need 5 -
// then I should only grab 5 here...
); ?>
This gets the other end of the array. I just want the oldest/ lesson-001, 002 etc.
With many combinations of reversing the array - and trying to get only a certain number - it's so slippery! and I always come up with an unexpected span of dates!
I drew up a quick proof of concept here: - but I feel like I'm fighting the framework. So, before I go any further... What do you think? What query options should I be using?
(just to clarify - those grayed out lessons are never printed to the HTML)
I would like to "Drip" lessons for my class - 1 a day. In the index for the lessons, on the first day, you would see the first lesson, and then on the second day, the second and first lesson, etc. - with today's lesson at the top (shown in the diagram).
I'm checking the current user - and finding out what 'team' they are on. that team has a start_date
- and so I can compare the start_date with today's date and get the difference. That's going well.
<?php $args = array(
'post_type' => 'lesson',
'orderby' => 'post_date', // or date?
'order'=> 'ASC',
'posts_per_page' => daysAvailable(), // difference between today/start date?
// if there are 100+ lessons... and I only need 5 -
// then I should only grab 5 here...
); ?>
This gets the other end of the array. I just want the oldest/ lesson-001, 002 etc.
With many combinations of reversing the array - and trying to get only a certain number - it's so slippery! and I always come up with an unexpected span of dates!
I drew up a quick proof of concept here: https://codepen.io/sheriffderek/pen/0d0237f0c352a8b42527cdbd4bff9c0e?editors=0011 - but I feel like I'm fighting the framework. So, before I go any further... What do you think? What query options should I be using?
(just to clarify - those grayed out lessons are never printed to the HTML)
Share Improve this question edited Mar 26, 2020 at 18:46 sheriffderek asked Mar 24, 2020 at 4:29 sheriffdereksheriffderek 1352 silver badges14 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 1So this may not exactly answer the question, but I hope it could help you, particularly with the following part in the question:
I just want the oldest/ lesson-001, 002 etc.
As I pointed in the comments, if the "lessons"/posts are dated chronologically like in the diagram (Jan 1st, Jan 14th, Feb 1st, etc.), you could just use the date query in WP_Query
and query for posts dated after the start_date
value and up until today's date.
So if for example the start_date
is 2020-01-01
(i.e. January 1st 2020) and today was February 1st 2020, the following query would return the January 1st
, January 14th
and February 1st
posts, which I suppose is what you wanted based on the question:
$args = array(
'post_type' => 'lesson',
'orderby' => 'date', // Note that 'date' is simply an alias for 'post_date'.
'order' => 'DESC',
'date_query' => array(
'after' => '2019-12-31', // this is start_date; equivalent to '2019-12-31 23:59:59'
'before' => 'tomorrow', // equivalent to using the value '2020-02-02 00:00:00'
),
);
$query = new WP_Query( $args );
// ... your code here ...
Note: The after
and before
parameters in the date query, they both accept a value that strtotime()
accepts (e.g. tomorrow
, yesterday
, +1 month
, etc.). Also, in WP_Query
, date
is the default orderby
value, while DESC
is the default order
value.
'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 3
would do it. But if you want the posts starting from the day they started the course, you can use date query, e.g.'date_query' => [ 'after' => '2020-03-22 06:59:59' ]
if the start date was on March 22nd at 7.00am. – Sally CJ Commented Mar 25, 2020 at 10:16