Skip to content

Instantly share code, notes, and snippets.

@troutcolor
Last active February 1, 2019 17:26
Show Gist options
  • Save troutcolor/ed3c72ea54a0ee8c814382eb24806cee to your computer and use it in GitHub Desktop.
Save troutcolor/ed3c72ea54a0ee8c814382eb24806cee to your computer and use it in GitHub Desktop.
simple shortcode plugin to display posts from the current day. I put it in a folder onthisday inside my plugins folder. Then make a page and add [onthisday] NB thewre are a few problems with thiis, use https://github.com/cogdog/wp-posted-today instead.
<?php
/*
There are some problems with this, use https://github.com/cogdog/wp-posted-today instead.
*/
/*
* Plugin Name: OnThisDay
* Description: shortcode for posts on the current day
* Version: 0.1
* Author: John Johnston
* Mostly borrowed from https://wordpress.stackexchange.com/questions/53462/on-this-day-php-code/53761
* Author URI: http://johnjohnston.info
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
function onthisday_register_shortcode() {
add_shortcode( 'onthisday' , 'onthisday_shortcode_routine' );
}
add_action( 'init', 'onthisday_register_shortcode' );
function onthisday_shortcode_routine( $args ) {
return on_this_day();
}
function on_this_day()
{
$on_this_day = array(
// Build the array using post date, and omit the year
'monthnum' => date( 'n' ),
'day' => date( 'j' )
);
//not sure I need this? JJ
//filter_where at foot of code, I think it is for use on a post and will avoid the current post.
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $on_this_day );
remove_filter( 'posts_where', 'filter_where' );
if ( $query->have_posts() ) {
$postYear = get_the_date( 'Y' );
while ( $query->have_posts() ) {
$query->the_post();
$year = get_the_time( 'Y' );
$queryYear = get_the_date( 'Y' );
$month = get_the_time( 'm' );
$day = get_the_time( 'd' );
if ( $queryYear == $postYear ) {
// I've comments this line out as I only want previous years JJ Should probably
//just sort filter_where function below
// I use permalinks to link to. If you'd rather link to the day archive list, use get_day_link( $year, $month, $day ) instead of get_permalink.
// This line compares the query year to the post year. If it's the same year, then add "Also from" so that it doesn't look like a duplicate link.
// echo '<a class="on-this-day" href="'.get_permalink( $query->ID ).'" title="On this day in '.$year.' - '.get_the_title().'">Also from '.$year.' - '.get_the_title().'</a><br> ';
} else {
echo '<p>On this day in <a class="on-this-day" href="'.get_permalink( $query->ID ).'" title="On this day in '.$year.' - '.get_the_title().'">'.$year.' - '.get_the_title().'</a></p> ';
}
}
// Reset post data so it displays the original post.
wp_reset_postdata();
} else {
// No posts to show
// echo "No Historical Posts On This Day";
echo "I don't seem to have posted on this day...";
}
}
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' )
{
// Posts for all years before the current post date.
//$where .= " AND post_date < '" . get_the_date( 'Y-m-d' ) . "'";
// Filter on post ID instead
$where .= " AND ID <> '" . get_the_ID() . "'";
return $where;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment