I’d like to cycle through existing posts in a post type, showing one per day ordered by title, and starting over when the last is reached

Question

Is there a way to feature one single custom post per day on the homepage, cycling through each of about twenty posts alphabetically and then starting over again from the first post?

I’m thinking the answer found here to a similar question might be the direction I need to go, but I’m not entirely certain: https://wordpress.stackexchange.com/a/325554/199183

This is the custom functioned I borrowed from the above answer:

add_action( 'wp', function () {
    if (! wp_next_scheduled ( 'mark_posts_as_featured_event' )) {
        wp_schedule_event(time(), 'daily', 'mark_posts_as_featured_event');
    }
} );

function mark_posts_as_featured_event_callback() {
    // if there are sticky posts in our CPT, unstick them
    $sticked_post_ids = get_option( 'sticky_posts' );
        $old_featured_posts = get_posts( array(
            'post_type' => 'member',
            'fields' => 'ids',
            'post__in' => $sticked_post_ids,
        ) );

        foreach ( $old_featured_post_ids as $post_id ) {
            unstick_post( $post_id );
        }
    }

    // stick new posts
    // get_random_posts
    $new_featured_post_ids = get_posts( array(
        'post_type' => 'member',
        'posts_per_page' => 1,
        'orderby' => 'title',
        'order' => 'DESC',
        'fields' => 'ids',
    ) );

    foreach ( $new_featured_post_ids as $post_id ) {
        stick_post( $post_id );
    } 

add_action( 'mark_posts_as_featured_event', 'mark_posts_as_featured_event_callback' );

And here is my query:

<?php 
$stickies = get_option( 'sticky_posts' );
// Make sure we have stickies to avoid unexpected output
if ( $stickies ) {
    $args = [
        'post_type'           => 'member',
        'post__in'            => $stickies,
        'posts_per_page'      => 1,
        'ignore_sticky_posts' => 1
    ];
    $the_query = new WP_Query($args);

    if ( $the_query->have_posts() ) { 
        while ( $the_query->have_posts() ) { 
            $the_query->the_post(); ?>

                   <div class="daily-member-header">
          <div class="featured-member-title">
              <div class="featured-label">Featured Member of the Day</div>
              <div class="featured-member-name"><?php the_title(); ?></div>
              <div class="featured-member-business"><?php the_field( 'business' ); ?></div>
              <div class=arrowcircle-cont"><img class="arrowcircle" src="/wp-content/uploads/2020/12/down-arrow.png" /></div>
          </div>
       </div>
        <div class="daily-member-logo"><img src="<?php the_field( 'logo' ); ?>" /></div>
       <div class="daily-member-content">
          <div class="daily-member-summary"><?php the_field( 'summary'); ?></div>
          <div class="daily-member-photo"><img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'medium' ); ?>" /></div>
       </div>
      <style>
         .daily-member-header{
         background:url('<?php the_field( 'background-img'); ?>');
         }
      </style>

       <?php  }    
        wp_reset_postdata();    
    }
} ?>
0
Matt Reeves 4 months 2020-12-18T17:10:38-05:00 0 Answers 6 views 0

Leave an answer

Browse
Browse