Every time you publish a new post, Google has no immediate way to know it exists unless it crawls your site or you tell it. By default, WordPress does nothing to notify search engines when new content goes live. This snippet fixes that by automatically pinging Google’s sitemap submission endpoint the moment a post is published, without any third-party plugin involved.
The Code
Add this to your theme’s functions.php file or a site-specific plugin. It hooks into WordPress’s publish_post action, which fires whenever a post transitions to the published state.
How It Works
The snippet uses WordPress’s built-in wp_remote_get() function to make an HTTP request to Google’s public ping endpoint: https://www.google.com/ping?sitemap=. This is a long-standing, documented mechanism Google provides for exactly this purpose. When Google receives the ping, it queues your sitemap for a fresh crawl, which in turn tells it about any new or updated URLs.
Two important arguments are passed to wp_remote_get(): 'blocking' => false and 'timeout' => 5. Setting blocking to false means WordPress fires the request and moves on immediately, it doesn’t wait for a response. This prevents any delay in the publishing experience on slow connections. The timeout of 5 seconds acts as a safety net in case the request does block for any reason.
The sitemap URL is built dynamically using get_bloginfo('url') so it always reflects your actual domain, even across staging and production environments.
When to Use This
This snippet is ideal for sites that publish content frequently and want to minimize the time between publishing and indexing. News sites, blogs with daily output, and content-heavy business sites all benefit from faster sitemap notification.
It works best when your site already has a valid sitemap at the expected URL. If you’re using an SEO plugin like SlimSEO, Yoast, or RankMath, your sitemap is typically available at /sitemap.xml or a similar path, confirm this before adding the snippet.
Things to Keep in Mind
This ping only notifies Google, it doesn’t guarantee immediate indexing. Google still decides when and whether to crawl based on your site’s crawl budget and authority. However, the ping does signal that something has changed, which generally results in faster crawling compared to waiting for Google to discover the update on its own.
Also note that this fires on every publish_post action, including updates to already-published posts. If you’re doing bulk edits or scheduled publishing, multiple pings may fire in quick succession. Google handles this gracefully, but if you want to limit it to first-time publishes only, you can add a check using get_post_meta to track whether a post has been pinged before.
Finally, this snippet targets the post post type by default via publish_post. If you also want to ping on custom post type publishes, replace the hook with transition_post_status and check for the specific post types you need.
add_action( 'publish_post', function( $post_id ) {
$sitemap = get_bloginfo( 'url' ) . '/sitemap.xml';
wp_remote_get(
'https://www.google.com/ping?sitemap=' . urlencode( $sitemap ),
[ 'blocking' => false, 'timeout' => 5 ]
);
}, 10, 1 );
