Support » Developing with WordPress » 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?

    • This topic was modified 1 year, 5 months ago by sheriffderek.
    • This topic was modified 1 year, 5 months ago by sheriffderek.
    • This topic was modified 1 year, 5 months ago by sheriffderek.
    • This topic was modified 1 year, 5 months ago by Jan Dembowski. Reason: Removed shortlink
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    @sheriffderek Side note: short links get expanded when found. They’ve been abused in the past.

    Moderator bcworkz

    (@bcworkz)

    I don’t quite grasp all of your criteria, but maybe this will help anyway. Assign a relative timestamp to each lesson. Keep the student’s signup timestamp in their meta data. Then you can query for accessible lessons for the current user by all lesson timestamps that are less than today’s timestamp minus the students start date.

    Thread Starter sheriffderek

    (@sheriffderek)

    @jdembowski – (gotcha – that makes sense) I was just trying to make it less ugly : )

    @bcworkz – The only problem with the relative time-stamps – is that lessons will be changed and added over time. If I needed to splice in a lesson – then I would have to change hundreds of dates. But’s that’s close to where I’m going. I guess the criteria is that the ‘lesson’ loop/query – gets an array of lessons based on their assiciated ‘unit’ – and prints that out. The scope doesn’t know about the bigger picture – so, I could have another loop on that page for all of the lessons – and gather some data there – and then maybe match the $ids to figure out their relative order

    Moderator bcworkz

    (@bcworkz)

    OK then, what if each lesson had a custom field specifying its previous lesson ID? Inserting a lesson would be a matter of altering one existing lesson’s field. Keep track of each student’s current progress into the plan by saving the current lesson’s ID and the date it was released to that student. When a student logs in, check this date to see if it’s the next day, and if so, give them the lesson whose custom field matches the ID saved in student/user meta, and update their meta data accordingly.

    If it’s not the next day, they get the lesson whose ID is stored in their meta data, and any lessons served previously, determined by “walking” up the chain of IDs referencing the previous lesson. The first lesson’s custom field would always have an ID of 0, so code knows when it’s reached the beginning.

    Not an original concept, this is how WP taxonomy term hierarchy is managed.

    Thread Starter sheriffderek

    (@sheriffderek)

    RE:

    what if each lesson had a custom field specifying its previous lesson ID

    This is interesting. I think that the order of the lessons may shift over time – but this would simplify those changes. I’ll have to try some things out and see what happens.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Finding total number of custom post type / and using it inside unique loop’ is closed to new replies.