Created
January 14, 2019 02:56
-
-
Save cogdog/64f9955679c7ab7d3346ad3bde16714f to your computer and use it in GitHub Desktop.
shortcode to output all posts published on current date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* outputs a list of all posts published in the current day in previous years | |
This will be done up properly as plugin, this is just current code inserted | |
in functions.php for CogDogBlog - see demo at https://cogdogblog.com/this-day | |
Inspired by John Johnston's version | |
https://gist.github.com/troutcolor/ed3c72ea54a0ee8c814382eb24806cee | |
*/ | |
add_action( 'init', 'cdb_postedtoday_shortcode' ); | |
function cdb_postedtoday_shortcode() { | |
add_shortcode( 'postedtoday' , 'cdb_postedtoday' ); | |
} | |
function cdb_postedtoday() { | |
// yep, today is today | |
$today = array( | |
'mon' => date( 'n', current_time( 'timestamp', 0 ) ), | |
'mday' => date( 'j', current_time( 'timestamp', 0 ) ), | |
'year' => date( 'Y', current_time( 'timestamp', 0 ) ) | |
); | |
// construct query for post with today's date and month, but also before today | |
$posts_from_today = new WP_Query( array( | |
'post_type' => 'post', | |
'date_query' => array( | |
array( | |
'month' => $today['mon'], | |
'day' => $today['mday'], | |
'before' => array ( | |
'month' => $today['mon'], | |
'day' => $today['mday'], | |
'year' => $today['year'], | |
) | |
), | |
), | |
'posts_per_page' => -1, | |
) ); | |
// so we can match by year | |
$year_tracker = $today['year']; | |
// a flag for the first entry | |
$first_year = true; | |
if ( $posts_from_today->have_posts() ) { | |
$output = '<p>There are <strong>' . $posts_from_today->found_posts . '</strong> posts found on this site published on ' . date("F j", strtotime( $today['mon'] . '/' . $today['mday'] . '/' . $today['year'] )) . '</p> <ul class="todaypost">'; | |
while( $posts_from_today->have_posts() ) { | |
$posts_from_today->the_post(); | |
$post_year = date("Y", strtotime( get_the_date() )); | |
if ( $post_year != $year_tracker ) { | |
// we have a new year to work with | |
// check if first year to show; if now we need to end previous list | |
if ( $first_year ) { | |
$endlist = ''; | |
$first_year = false; | |
} else { | |
$endlist = '</li></ul>'; | |
} | |
// ok make the list for this yet | |
$output .= $endlist . '<li><strong>' . date("F j, Y", strtotime( $today['mon'] . '/' . $today['mday'] . '/' . $post_year )) . '</strong><ul>'; | |
// now track this year | |
$year_tracker = $post_year; | |
} | |
// output post and link | |
$output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>: <span class="today_excerpt">' . get_the_excerpt() . '</span></li>'; | |
} | |
$output .= '</ul>'; | |
} else { | |
$output = 'No posts were found published on ' . date("F j", strtotime( $today['mon'] . '/' . $today['mday'] . '/' . $today['year'] )); | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has been improved with more options and now is a full blown plugin https://github.com/cogdog/wp-posted-today