Using Multisite Sitemaps with Custom Post Type

I installed the plugin as suggested in your page. It seems to work, but only to default wordpress posts and pages. The custom posts (CPT) not being taken into account to generate the sitemap.
How to ensure that the generator will consider the custom posts types?

The URL: http://demo.imobilix.com.br/sitemap.xml

  • Milan

    Hi Jefferson,

    I hope you are having a good day and thanks for asking us. :slight_smile:

    Currently this plugin doesn't support custom post-types, however I have found a thread where the developers discusses & provides code to add to your themes functions.php to achieve this.

    function my_sitemap_replacement ($content) {
    	//return $content . '<empty>Nothing here</empty>';
    	$totalposts = apply_filters('simple_sitemaps-totals_soft_limit', (defined('SIMPLE_SITEMAPS_POST_SOFT_LIMIT') ? SIMPLE_SITEMAPS_POST_SOFT_LIMIT : 50));
    	$latestposts = $totalposts ? get_posts( 'post_type=MY_POST_TYPE&numberposts=' . $totalposts . '&orderby=date&order=DESC' ) : array();
    	foreach ( $latestposts as $post ) {
    		$content .= "	<url>\n";
    		$content .= '		<loc>' . get_permalink( $post->ID ) . "</loc>\n";
    		$content .= '		<lastmod>' . mysql2date( 'Y-m-d\TH:i:s', $post->post_modified_gmt ) . "+00:00</lastmod>\n";
    		$content .= '		<priority>' . number_format( 1, 1 ) . "</priority>\n";
    		$content .= "	</url>\n";
    	}
    	return $content;
    }
    add_filter('simple_sitemaps-generated_urlset', 'my_sitemap_replacement');

    https://premium.wpmudev.org/forums/topic/sitemaps-and-custom-post#post-239792

    It's bit old thread but I've ensured that it was given in context of current Multisite Sitemaps plugin. So hit it and let me know how it goes in your case. :slight_smile:

    Enjoy WPMU DEV.
    Best Regards,
    Milan

  • Milan

    Hello Jefferson ,

    I hope you are having a good day. :slight_smile:

    I think this way we can tweak above snippet to make it support array of custom post types. :slight_smile:

    function my_sitemap_replacement ($content) {
    	//return $content . '<empty>Nothing here</empty>';
    	$totalposts = apply_filters('simple_sitemaps-totals_soft_limit', (defined('SIMPLE_SITEMAPS_POST_SOFT_LIMIT') ? SIMPLE_SITEMAPS_POST_SOFT_LIMIT : 50));
    
    	$customPostTypes = array(
    		'MY_POST_TYPE_ONE',
    		'MY_POST_TYPE_TWO',
    		'MY_POST_TYPE_THREE',
    		);
    
    	foreach ($customPostTypes as $postType) {
    		$latestposts = $totalposts ? get_posts( 'post_type='. $postType .'&numberposts=' . $totalposts . '&orderby=date&order=DESC' ) : array();
    		foreach ( $latestposts as $post ) {
    			$content .= "	<url>\n";
    			$content .= '		<loc>' . get_permalink( $post->ID ) . "</loc>\n";
    			$content .= '		<lastmod>' . mysql2date( 'Y-m-d\TH:i:s', $post->post_modified_gmt ) . "+00:00</lastmod>\n";
    			$content .= '		<priority>' . number_format( 1, 1 ) . "</priority>\n";
    			$content .= "	</url>\n";
    		}
    	}
    
    	return $content;
    }
    add_filter('simple_sitemaps-generated_urlset', 'my_sitemap_replacement');

    Post Update:
    Above snippet may cause tiny issue with performance of site, so please go with this one instead. :slight_smile:

    function my_sitemap_replacement ($content) {
    	//return $content . '<empty>Nothing here</empty>';
    	$totalposts = apply_filters('simple_sitemaps-totals_soft_limit', (defined('SIMPLE_SITEMAPS_POST_SOFT_LIMIT') ? SIMPLE_SITEMAPS_POST_SOFT_LIMIT : 50));
    	$arguments = array(
    					'post_type' 	=> array( 'MY_POST_TYPE_ONE', 'MY_POST_TYPE_TWO', 'MY_POST_TYPE_THREE' ),
    					'numberposts'	=> $totalposts,
    					'orderby'		=> 'date',
    					'order'			=> 'DESC'
    					);
    	$latestposts = $totalposts ? get_posts( $arguments ) : array();
    	foreach ( $latestposts as $post ) {
    		$content .= "	<url>\n";
    		$content .= '		<loc>' . get_permalink( $post->ID ) . "</loc>\n";
    		$content .= '		<lastmod>' . mysql2date( 'Y-m-d\TH:i:s', $post->post_modified_gmt ) . "+00:00</lastmod>\n";
    		$content .= '		<priority>' . number_format( 1, 1 ) . "</priority>\n";
    		$content .= "	</url>\n";
    	}
    	return $content;
    }
    add_filter('simple_sitemaps-generated_urlset', 'my_sitemap_replacement');

    Let me know how this goes. :slight_smile:
    Cheers,
    Milan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.