Hi, thanks for reporting this!
The news sitemap is set up as a feed so indeed the is_feed()
conditional applies. Try changing this line:
if ( is_feed() ) {
to:
if ( is_feed() && ( ! function_exists( 'is_sitemap' ) || ! is_sitemap() ) ) {
Please let me know 🙂
-
This reply was modified 2 months, 3 weeks ago by Rolf Allard van Hagen. Reason: missing ) in the code suggestion
Hello,
Thank you for the response.
We checked your solution, but unfortunately, it is not working. The news site map still doesn’t display the latest news.
Do we have a function called is_sitemap() in WordPress? Do you think that will work?
In the meantime I created a function to exclude the sitemap by using its slug, Please check below code.
function wpb_snippet_publish_later_on_feed( $where ) {
global $wpdb;
if ( is_feed() && !is_admin() && !is_sitemap_request() ) {
// Timestamp in WP-format.
$now = gmdate( 'Y-m-d H:i:s' );
// Number of units to wait.
$wait = '20'; // integer.
// Choose time unit.
$unit = 'MINUTE'; // MINUTE, HOUR, DAY, WEEK, MONTH, YEAR.
// Add SQL-syntax to default $where.
$where .= " AND TIMESTAMPDIFF($unit, $wpdb->posts.post_date_gmt, '$now') > $wait ";
}
return $where;
}
add_filter( 'posts_where', 'wpb_snippet_publish_later_on_feed' );
// Function to check if the current request is for a sitemap.
function is_sitemap_request() {
$request_uri = $_SERVER['REQUEST_URI'];
// Check for specific sitemap URL
if ( strpos($request_uri, 'sitemap-news.xml') !== false ) {
return true;
}
return false;
}
Can you please check and make sure it is working fine with the plugin? I tested it on my development site and it is working like the way I mentioned (delay only for feeds and the delay does not affect the sitemaps).
Please check and let me know your thoughts.
Thank you 🙂
Do we have a function called is_sitemap() in WordPress? Do you think that will work?
No, but the function is in the sitemap plugin. But I now realize is should have suggested the plugin function is_news()
instead:
if ( is_feed() && ( ! function_exists( 'is_news' ) || ! is_news() ) ) {
But you can continue using your own (working) code just fine too 🙂
Hello @ravanh ,
Thank you for the solution.
This solution worked 🙂
Thanks again