I was creating a WordPress plugin and needed to get the time (specifically the year) posts were written or published.
Thanks to get_post_time function, i was able to get the time posts were published.
Here is a my plugin function that returns the year posts were published.
< ?php
function displayNotification($content) {
global $post;
//get post year eg 2014
$post_year = get_post_time('Y', true, $post -> ID);
return $content;
}
UPDATE: am sorry for not explaining what the role of $content
in the code.
I added it because i need the code above to get post years as the plugin i built needed to check the year of post against the current year to see if the post is older than a given number of years and then display some sort of message above post content.
Thus, the a filter needed to be hooked to the_content
filter to achieve the purpose.
Below is the filter hook.
add_filter('the_content', 'displayNotification');
Take note, the first argument of get_post_time
is PHP time and date character therefore, you can not only retrieve the post year, but also the month, date or mixed time & date format as specified by the date/time format character.
Just hope someone find this useful.