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:
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();
}
} ?>
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/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();
}
} ?>
Share
Improve this question
edited Dec 21, 2020 at 18:04
Matt Reeves
asked Dec 18, 2020 at 22:40
Matt ReevesMatt Reeves
112 bronze badges
1 Answer
Reset to default 0To summarize, I'm creating a custom WP Schedule event that checks once a day to see if any of my "member" custom post type posts are marked as sticky, and if so, removes them from the stickied post options table, then it randomly assigns another single member post as sticky. (I'm displaying this random post on the homepage via a shortcode, shortcode code not shown here).
I think I've made some progress on this. My code had a few errors in it that I've highlighted below, such as the wrong name on a variable and a missing opening if statement.
I had to forego displaying these posts in alphabetical order because I couldn't figure out how to code that, so I've settled on random order. I added php session functionality to the orderby rand so that the individual site visitor won't see the same post twice, although I'm not completely sure this is going to work.
Here is my query with the help from this post:https://wordpress.stackexchange/a/217080/199183
<?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();
}
} ?>
And here are the complete functions with help from this post - https://wordpress.stackexchange/a/325554/199183 (for the scheduling functionality and this page - https://love-coding.pl/en/wordpress-how-to-display-random-posts-without-duplicates/ (for the session functionality):
function prefix_start_session() {
if( !session_id() ) {
session_start();
}
}
add_action( 'init', 'prefix_start_session' );
function get_random_post() {
if ( !isset( $_SESSION['random'] ) ) {
$_SESSION['random'] = rand();
}
return $_SESSION['random'];
}
function wi_custom_cron_schedule( $schedules ) {
$schedules['5min'] = array(
'interval' => 5, // Every 5 minutes
'display' => __( 'Every 5 Minutes' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'wi_custom_cron_schedule' );
if (! wp_next_scheduled ( 'mark_posts_as_featured_event' )) {
wp_schedule_event(time(), '5min', 'mark_posts_as_featured_event');
}
add_action( 'mark_posts_as_featured_event', 'mark_posts_as_featured_event_callback' );
function mark_posts_as_featured_event_callback() {
// if there are sticky posts in our CPT, unstick them
$sticked_post_ids = get_option( 'sticky_posts' );
if ( ! empty($sticked_post_ids) ) {
$old_featured_posts = get_posts( array(
'post_type' => 'member',
'fields' => 'ids',
'post__in' => $sticked_post_ids,
) );
foreach ( $old_featured_posts as $post_id ) {
unstick_post( $post_id );
}
}
// stick new posts
// get_random_posts
$new_featured_posts = get_posts( array(
'post_type' => 'member',
'posts_per_page' => 1,
'orderby' => 'rand(' . get_random_post() . ')',
'fields' => 'ids',
) );
foreach ( $new_featured_posts as $post_id ) {
stick_post( $post_id );
}
}
Errors fixed
In the previous code I realized I need to change:
foreach ( $old_featured_post_ids as $post_id ) {
to:
foreach ( $old_featured_posts as $post_id ) {
Also realized I was missing the opening if statement.
Old:
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 );
}
}
new:
function mark_posts_as_featured_event_callback() {
// if there are sticky posts in our CPT, unstick them
$sticked_post_ids = get_option( 'sticky_posts' );
if ( ! empty($sticked_post_ids) ) {
$old_featured_posts = get_posts( array(
'post_type' => 'member',
'fields' => 'ids',
'post__in' => $sticked_post_ids,
) );
foreach ( $old_featured_posts as $post_id ) {
unstick_post( $post_id );
}
}
New Code added
function prefix_start_session() {
if( !session_id() ) {
session_start();
}
}
add_action( 'init', 'prefix_start_session' );
function get_random_post() {
if ( !isset( $_SESSION['random'] ) ) {
$_SESSION['random'] = rand();
}
return $_SESSION['random'];
}
And changed 'orderby- => 'rand', to 'orderby' => 'rand(' . get_random_post() . ')', to make use of the new session code.