The get_lastpostmodified() is a WordPress builtin function to get the most recent time that a post on the site was modified. It will return the timestamp in ‘Y-m-d H:i:s’ format.
The server timezone is the default and is the difference between GMT and server time. The ‘blog’ value is just when the last post was modified. The ‘gmt’ is when the last post was modified in GMT time.
Syntax:
1 | get_lastpostmodified( string $timezone = 'server', string $post_type = 'any' ) |
1 2 3 4 5 6 7 8 9 10 | public function test_add_to_index() { $output = $this->instance->add_to_index(''); $output_date = new DateTime(get_lastpostdate('gmt'), new DateTimeZone(new WPSEO_News_Sitemap_Timezone())); $expected_output = '<sitemap>' . "\n"; $expected_output .= '<loc>' . home_url('news-sitemap.xml') . '</loc>' . "\n"; $expected_output .= '<lastmod>' . htmlspecialchars($output_date->format('c')) . '</lastmod>' . "\n"; $expected_output .= '</sitemap>' . "\n"; $this->assertEquals($expected_output, $output); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Handles daily ping */ public function SendPingDaily() { $this->LoadOptions(); $blogUpdate = strtotime(get_lastpostdate('blog')); $lastPing = $this->GetOption('i_lastping'); $yesterday = time() - 60 * 60 * 24; if ($blogUpdate >= $yesterday && ($lastPing == 0 || $lastPing <= $yesterday)) { $this->SendPing(); } if ($this->GetOption('b_stats')) { $this->SendStats(); } } |
Reference: https://developer.wordpress.org/reference/functions/get_lastpostmodified/
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.