Daily Posts plugin
-
Hi
I had this idea a while ago to create a plugin to show the posts on today excluding category and year. I asked Gemini to help with the code and this is what it has come up with:
<?php
/**
* Shortcode to display posts published on the current day and month, across all years.
*
* Usage: [daily_on_this_day_posts]
*/
function display_daily_posts_on_this_day_shortcode() {
// Get the current day and month
// 'j' for day of the month without leading zeros (1 to 31)
// 'n' for month number without leading zeros (1 to 12)
$current_day = date('j');
$current_month = date('n');
// Arguments for the WordPress query
$args = array(
'post_type' => 'post', // We are querying standard posts
'post_status' => 'publish', // Only show published posts
'posts_per_page' => -1, // Retrieve all posts that match the criteria
'date_query' => array( // This is the key part for matching the date
array(
'month' => $current_month, // Match the current month
'day' => $current_day, // Match the current day
// Year is intentionally omitted to match across all years
),
),
'orderby' => 'date', // Order the results by their full publication date
'order' => 'DESC', // Show the newest posts first (e.g., from most recent year)
'ignore_sticky_posts' => 1, // Do not let sticky posts interfere with the date order
// By default, WP_Query searches all categories, so no need to specify category exclusion explicitly
);
$daily_posts_query = new WP_Query($args);
// Prepare the output string
$output = '';
// Get the current date formatted nicely for the title (e.g., "May 12th")
$current_date_formatted = date('F jS'); // Using 'F jS' for "Month daySuffix", e.g., "May 12th"
if ($daily_posts_query->have_posts()) {
$output .= '<h2>Posts from ' . esc_html($current_date_formatted) . ' (Across All Years)</h2>';
// Removed the <ul> tag
while ($daily_posts_query->have_posts()) {
$daily_posts_query->the_post();
// Each post is now a separate div block
$output .= '<div class="daily-post-entry" style="margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid #eee; overflow: hidden;">'; // Added overflow:hidden to contain floats
// Display the featured image if it exists, floated to the left
if (has_post_thumbnail()) {
// You can change 'thumbnail' to other sizes like 'medium', 'large', or a custom size
// Added float:left and margin for spacing
$output .= '<div class="daily-post-image" style="float: left; margin-right: 15px; margin-bottom: 10px; max-width: 150px;">'; // Added max-width to control image size
$output .= get_the_post_thumbnail(get_the_ID(), 'thumbnail'); // Using 'thumbnail' size
$output .= '</div>';
}
// Container for the text content, to allow it to flow next to the image
$output .= '<div class="daily-post-content" style="overflow: hidden;">'; // overflow:hidden helps contain content next to float
// Display the post title as a link
$output .= '<h3><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></h3>';
// Display the full publication date
$output .= '<p style="font-size: 0.9em; color: #555; margin-top: 0;">Published on: ' . esc_html(get_the_date('F jS, Y')) . '</p>'; // e.g., May 12th, 2023
// Display the excerpt
$output .= '<div style="margin-top: 5px;">' . get_the_excerpt() . '</div>';
$output .= '</div>'; // End of daily-post-content
// $output .= '<div style="clear: both;"></div>'; // Optional: explicit clear if overflow:hidden on parent isn't enough in some themes
$output .= '</div>'; // End of daily-post-entry
}
// Removed the </ul> tag
wp_reset_postdata(); // Important: Restore original post data after custom loop
} else {
$output .= '<p>No posts found for ' . esc_html($current_date_formatted) . ' from any year.</p>';
}
return $output; // Return the HTML to be displayed by the shortcode
}
// Register the shortcode so WordPress recognizes it
add_shortcode('daily_on_this_day_posts', 'display_daily_posts_on_this_day_shortcode');
?>Should I simply post the code in the functions file or would it be better to create a plugin and update on a regular basis please? Is the code ok?
Thanks
Rich
The page I need help with: [log in to see the link]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘Daily Posts plugin’ is closed to new replies.