0

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?

enter image description here

(just to clarify - those grayed out lessons are never printed to the HTML)

7
  • Sorry, what's the question? What are you having trouble with?
    – Sally CJ
    Mar 25, 2020 at 6:46
  • @SallyCJ - sorry it's such a jumble! Maybe I should split it up into two questions. Let's say I have 30 posts. The student may have started the course 3 days ago - so, they should only see 3 posts. That would mean the oldest three posts. Please see the last diagram. (additionally - I'd like to insert some 'days-off' but that is probably another question.) Mar 25, 2020 at 7:01
  • If you just want to display the most recent posts, something like '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
    Mar 25, 2020 at 10:16
  • The dates for the posts are relative to the start date / so, they aren't the real dates that they are posted on. Those just serve to keep them in order. I'll check out the date query specifics. Thanks. I've also added a regular PHP example image - in case that helps explain. Mar 25, 2020 at 19:31
  • 1
    OK! So, I spent some time with the date_query - and the meta_query - and the 'compare' option - and that is very very helpful!. I'm going to break this into a second question about adding in other objects to the post list, but maybe you can answer the question wih a suggestion that using the query - I can get what I want. : ) Mar 26, 2020 at 18:39

1 Answer 1

1

So 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.

Your Answer

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

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