Creating dynamic or automated XML news sitemap for WordPress is easy. Which will automatically create a news sitemap generator through PHP function code without any plugins. Which will auto generate news articles available in WordPress post, category or custom post type in news-sitemap.xml.
If your website is a news website or covers news on some topics. So to appear in the search results of news, it is very important to have a news sitemap on your website. If you do not use this function then the news articles available on your website will never rank in news search results. Be it Google search or Bing’s news search result.
What is Dynamic XML News Sitemap
Dynamic XML News Sitemap is a roadmap of news articles written on the website. Which is another form of sitemap.xml file. But the content in it is kept on the basis of news publication. Which contains data of publication, publisher name, article date, and image etc. Which informs Google News Publisher Bot about the existence of a news article.
Benefits of XML News Sitemap without Plugin
Creating dynamic XML news sitemap on WordPress without plugins is very beneficial. It protects your site from heavy plugin code on your website. It also reduces load time and speeds up your website performance. Apart from this, it increases the security of the site and avoids the drop in ranking due to bad updates.
Creating News Sitemap Generator Functionality
Here we will write a PHP function code for Dynamic XML News Sitemap sitemap generator which can be invoked in functions.php of WordPress theme editor. Or you can force it in a code snippet. To ensure that your website is secure, we recommend code snippets, so let’s create a good news sitemap in a few steps.
Step 1 – Creating a PHP Function File
<?php
function dynamic_news_sitemap_init() {
add_rewrite_rule( '^news-sitemap.xml$', 'index.php?dynamic_news_sitemap=1', 'top' );
}
add_action( 'init', 'dynamic_news_sitemap_init' );
function dynamic_news_sitemap_query_vars( $query_vars ) {
$query_vars[] = 'dynamic_news_sitemap';
return $query_vars;
}
add_filter( 'query_vars', 'dynamic_news_sitemap_query_vars' );
function dynamic_news_sitemap_template() {
if ( get_query_var( 'dynamic_news_sitemap' ) ) {
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">';
$time_48_hours_ago = date( 'Y-m-d H:i:s', strtotime( '-48 hours' ) );
$args = array(
'post_type' => 'post', // Change this if your news posts are of a different post type
'category_name' => 'your-category-slug', // Include posts only from this category
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'date_query' => array(
'after' => $time_48_hours_ago,
),
);
$news_posts = new WP_Query( $args );
if ( $news_posts->have_posts() ) {
while ( $news_posts->have_posts() ) {
$news_posts->the_post();
echo '<url>';
echo '<loc>' . esc_url( get_permalink() ) . '</loc>';
echo '<news:news>';
echo '<news:publication>';
echo '<news:name>' . esc_html( get_bloginfo( 'name' ) ) . '</news:name>';
echo '<news:language>' . esc_html( get_bloginfo( 'language' ) ) . '</news:language>';
echo '</news:publication>';
echo '<news:publication_date>' . get_the_date( 'c' ) . '</news:publication_date>';
echo '<news:title>' . esc_html( get_the_title() ) . '</news:title>';
echo '</news:news>';
echo '</url>';
}
wp_reset_postdata();
}
echo '</urlset>';
exit;
}
}
add_action( 'template_redirect', 'dynamic_news_sitemap_template' );
Some of the following functions have been added to the PHP code generated above:
- Post Getting: Post query for news URL has been added. You will be able to select any category related to the post to it.
- Sitemap URL Making: The URL of news-sitemap.xml will automatically assert itself from your home URL. Which can be seen by visiting yoursite.com/news-sitemap.xml
- Remove News: According to Google’s algorithm, your news article will be visible in the XML news sitemap only for 48 hours. After 48 hours the news article will be automatically removed from the sitemap.
- Custom Post: Features of selecting news for your custom post have been added to this function.
- Data inside News sitemap: The title, URL, language, publication, and publication date of the news article are shown inside the news sitemap.
Step 2 – Select news category according to your website.
Make these changes in the code to select the news category or news location built on the website.
'post_type' => 'post', // Change this if your news posts are of a different post type
'category_name' => 'News', // Include posts only from this category
Will fetch ‘post‘ query in post_type. And in category select your ‘news‘ category in category_name. If you want to select a custom post field, then input the url of the custom post type instead of ‘Post‘.
Step 3 – Adding Functions to WordPress
To implement the news sitemap in WordPress, you can paste the code written in the function PHP of the theme. But I would recommend you simple and safe way to add it in WPCode snippet. For this follow these steps.
- Go to the Plugins section in the WordPress dashboard and click Add New Plugin.
- Search for WPCode snippet in the search box
- Install WPCode Plugin
- Now go to the Code Snippets section in the WordPress dashboard and click on Add Snippet
- Select Add Your Custom Code (New Snippet) to add a new PHP function
- Type title name and select PHP snippet in Code Type
- Now paste the above code in Code Preview and save it
- Change Inactive to Active to start PHP Function
- Wait for a few seconds for the process to complete
Step 4 – Testing and Validating the News Sitemap
To see the news sitemap created on your website, check for example: yoursite.com/news-sitemap.xml and check the XML Sitemap Validator for any errors in the site.
Step 5 – Clear cache data
This is the final important step. If you are using any cache plugin in WordPress or using Cloudflare then definitely clear the cache file. This function will work properly. Otherwise you may see some errors.
Creating an automated news sitemap on WordPress is a best SEO practice. It protects your website from other third party plugins and heavy codes and provides security. If you are facing some problems in adding this news sitemap to your WordPress, then write in the comment below.