Finding total number of custom post type / and using it inside unique loop
-
I have a set of ‘lessons’ that are associated with ‘units.’
In total – you could say there were 36 lessons. I want to drip them (make them available based on date) to my students.
I have a little proof of concept to get the total amount of lessons each student should have available: https://codepen.io/sheriffderek/pen/0d0237f0c352a8b42527cdbd4bff9c0e?editors=1011 based on their user signup date. (manually added users)
So, that’s nice – but the part I’m having a hard time wrapping my mind around – is that those lessons have associations to the units and are on unit index pages.
Here are the lesson index templates for the unit pages.
The goal is – if you started on the 1st / and it was the 9th, then you’d have access to 8 lessons. To be more specific… you’d have access to unit 1 (4 videos) and unit 2 (4 videos) – but not unit 3 yet. I want these lessons to be aware of the total lessons – and the amount that will be available on a given date. But then – also the units would have to be aware of their index list.
<?php $lessons = get_posts(array( 'post_type' => 'lesson', 'orderby' => 'date', 'order'=> 'ASC', 'meta_query' => array( array( 'key' => 'belongs_to_unit', // name of custom field 'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234" 'compare' => 'LIKE' ) ) )); ?> <?php if ($lessons): ?> <ol class='lesson-list'> <?php foreach( $lessons as $index => $lesson ): ?> <?php $standardTitle = $lesson->post_title; $formattedTitle = get_field('formatted_title', $lesson->ID); $hasHormattedTitle = get_field('has_formatted_title', $lesson->ID); ?> <li class='lesson'> <article class='other-content'> <h3 class='unit'>Lesson: <?php echo($index + 1); ?></h3> <h2 class='title'> <?= ($hasHormattedTitle) ? $formattedTitle : $standardTitle ?> </h2> <p>redacted ui</p> </article> </li> <?php endforeach; ?> </ol> <?php else: ?> There are no lessons in this unit yet! <?php endif; ?>
How can I build a mental model for this?
- The topic ‘Finding total number of custom post type / and using it inside unique loop’ is closed to new replies.