Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active September 8, 2022 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/0560252f5afba3035a76f6e39d213298 to your computer and use it in GitHub Desktop.
Save damiencarbery/0560252f5afba3035a76f6e39d213298 to your computer and use it in GitHub Desktop.
Order posts by post meta dates: Storing dates in post meta enables easy grouping of posts into current, future and past. This automatic grouping simplifies the client workflow and helps site visitors. https://www.damiencarbery.com/2018/12/order-posts-by-post-meta-dates/
<?php
/*
Plugin Name: Order posts by post meta dates
Plugin URI: https://www.damiencarbery.com/2018/12/order-posts-by-post-meta-dates/
Description: Storing dates in post meta enables easy grouping of posts into current, future and past. This automatic grouping simplifies the client workflow and helps site visitors.
Author: Damien Carbery
Version: 0.2
*/
// Allow entering start and end dates for Family Focus and Tailored Training training sessions.
// This will allow ordering of posts and sorting into current/forthcoming/past.
add_action( 'cmb2_admin_init', 'cad_training_dates_metaboxes' );
function cad_training_dates_metaboxes() {
$cmb = new_cmb2_box( array(
'id' => 'cad_training_dates',
'title' => 'Training Dates',
'object_types' => array( 'post', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
) );
$cmb->add_field( array(
'name' => 'Enter start and end dates', // This is not needed here.
'desc' => 'Complete this section for "Family Focus" and "Tailored Training" posts. Ignore it for other posts.',
'type' => 'title',
'id' => 'training_dates_title'
) );
$cmb->add_field( array(
'name' => 'Start Date',
'desc' => 'What date does this training start?',
'id' => 'training_start',
'type' => 'text_date_timestamp',
'date_format' => 'j M Y',
) );
$cmb->add_field( array(
'name' => 'End Date',
'desc' => 'What date does this training end?',
'id' => 'training_end',
'type' => 'text_date_timestamp',
'date_format' => 'j M Y',
) );
}
// Verify that CMB2 plugin is active.
add_action( 'admin_notices', 'verify_cmb2_active' );
function verify_cmb2_active() {
if ( ! defined( 'CMB2_LOADED' ) ) {
$plugin_data = get_plugin_data( __FILE__ );
$plugin_name = $plugin_data['Name'];
?>
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo $plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div>
<?php
//error_log( 'CMB2 is not active.' );
}
}
<?php
// Display the start and end dates of a Family Focus or Tailored Training course at the top of the post.
add_action( 'genesis_entry_content', 'cad_course_dates_on_single_post', 5 );
function cad_course_dates_on_single_post() {
if ( ! is_single() ) {
return;
}
global $post;
$start_date = get_post_meta( get_the_ID(), 'training_start', true );
$end_date = get_post_meta( get_the_ID(), 'training_end', true );
if ( ! empty( $start_date ) && ! empty( $end_date ) ) {
echo '<p>Start date: ', date( 'l j M Y', $start_date );
echo '<br />End date: ', date( 'l j M Y', $end_date ), '</p>';
}
}
<?php
// Sort Family Focus and Tailored Training courses into Current/Forthcoming/Past.
add_shortcode( 'training-schedule', 'cad_training_courses_posts' );
function cad_training_courses_posts( $atts, $content, $code ) {
$atts = shortcode_atts( array(
'category' => '',
'image_size' => 'medium',
), $atts );
// A category must be specified.
if ( empty( sanitize_text_field( $atts[ 'category' ] ) ) ) {
return '';
}
$now = time();
$category = sanitize_text_field( $atts[ 'category' ] );
$image_size = sanitize_key( $atts[ 'image_size' ] );
$current_courses = array();
$forthcoming_courses = array();
$past_courses = array();
$args = array( 'post_type' => 'post', 'category_name' => $category, 'posts_per_page' => -1 );
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
global $post;
$permalink = get_permalink();
$title = $title = '<a class="title" href="' . apply_filters( 'the_permalink', $permalink ) . '">' . apply_filters( 'the_title', get_the_title() ) . '</a>';;
$image = '';
if ( $image_size && has_post_thumbnail() ) {
$image = '<a class="image" href="' . $permalink . '">' . get_the_post_thumbnail( $post->ID, $image_size ) . '</a> ';
}
$start_date = get_post_meta( get_the_ID(), 'training_start', true );
$end_date = get_post_meta( get_the_ID(), 'training_end', true );
// Set a default as some posts may not have set the start and end dates.
if ( empty( $start_date ) ) {
$start_date = 0;
}
if ( empty( $end_date ) ) {
$end_date = 0;
}
$course_markup = '<div class="listing-item">' . $image . $title;
// Determine whether the course is past, ongoing (current) or future (forthcoming).
// Include the date if it was stored in post meta.
if ( $now < $start_date ) {
if ( $start_date > 0 && $end_date > 0 ) {
$forthcoming_courses[] = $course_markup . sprintf( '<p>Runs: %s until %s</p>', date( 'l j M Y', $start_date ), date( 'l j M Y', $end_date ) ) .'</div>';
}
else {
$forthcoming_courses[] = $course_markup .'</div>';
}
}
else {
if ( $now < $end_date ) {
if ( $start_date > 0 && $end_date > 0 ) {
$current_courses[] = $course_markup . sprintf( '<p>Started: %s,<br />ends %s</p>', date( 'l j M Y', $start_date ), date( 'l j M Y', $end_date ) ) .'</div>';
}
else {
$current_courses[] = $course_markup .'</div>';
}
}
else {
if ( $start_date > 0 && $end_date > 0 ) {
$past_courses[] = $course_markup . sprintf( '<p>Ran: %s until %s</p>', date( 'l j M Y', $start_date ), date( 'l j M Y', $end_date ) ) .'</div>';
}
else {
$past_courses[] = $course_markup .'</div>';
}
}
}
}
wp_reset_postdata();
$courses_markup = '';
if ( ! empty( $current_courses ) ) {
$courses_markup .= '<h3 class="course_header">Current Courses</h3><div class="display-posts-listing">' . join( '', $current_courses ) . '</div>';
}
if ( ! empty( $forthcoming_courses ) ) {
$courses_markup .= '<h3 class="course_header">Forthcoming Courses</h3><div class="display-posts-listing">' . join( '', $forthcoming_courses ) . '</div>';
}
if ( ! empty( $past_courses ) ) {
$courses_markup .= '<h3 class="course_header">Past Courses</h3><div class="display-posts-listing">' . join( '', $past_courses ) . '</div>';
}
return $courses_markup;
}
else {
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment