<?php | |
/** | |
* Create a new custom yoast seo sitemap | |
*/ | |
add_filter( 'wpseo_sitemap_index', 'ex_add_sitemap_custom_items' ); | |
add_action( 'init', 'init_wpseo_do_sitemap_actions' ); | |
// Add custom index | |
function ex_add_sitemap_custom_items(){ | |
global $wpseo_sitemaps; | |
$date = $wpseo_sitemaps->get_last_modified('CUSTOM_POST_TYPE'); | |
$smp =''; | |
$smp .= '<sitemap>' . "\n"; | |
$smp .= '<loc>' . site_url() .'/CUSTOM_KEY-sitemap.xml</loc>' . "\n"; | |
$smp .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n"; | |
$smp .= '</sitemap>' . "\n"; | |
return $smp; | |
} | |
function init_wpseo_do_sitemap_actions(){ | |
add_action( "wpseo_do_sitemap_CUSTOM_KEY", 'ex_generate_origin_combo_sitemap'); | |
} | |
function ex_generate_origin_combo_sitemap(){ | |
global $wpdb; | |
global $wp_query; | |
global $wpseo_sitemaps; | |
$post_type = 'CUSTOM_POST_TYPE'; | |
wp_reset_query(); | |
$args = array( | |
'posts_per_page' => -1, | |
'orderby' => 'post_date', | |
'order' => 'DESC', | |
'post_type' => $post_type, | |
'post_status' => 'publish', | |
'suppress_filters' => true | |
); | |
query_posts( $args ); | |
wp_reset_postdata(); | |
//echo '<pre>';print_r($url);echo '</pre>'; | |
$posts_array = get_posts( $args ); | |
$output = ''; | |
if( !empty( $posts_array ) ){ | |
$chf = 'weekly'; | |
$pri = 1.0; | |
foreach ( $posts_array as $p ) { | |
$p->post_type = $post_type; | |
$p->post_status = 'publish'; | |
$p->filter = 'sample'; | |
$url = array(); | |
if ( isset( $p->post_modified_gmt ) && $p->post_modified_gmt != '0000-00-00 00:00:00' && $p->post_modified_gmt > $p->post_date_gmt ) { | |
$url['mod'] = $p->post_modified_gmt; | |
} else { | |
if ( '0000-00-00 00:00:00' != $p->post_date_gmt ) { | |
$url['mod'] = $p->post_date_gmt; | |
} else { | |
$url['mod'] = $p->post_date; | |
} | |
} | |
$url['loc'] = site_url().'/sample/all/'.$p->post_name; | |
$url['chf'] = $chf; | |
$url['pri'] = $pri; | |
$output .= $wpseo_sitemaps->sitemap_url( $url ); | |
// Clear the post_meta and the term cache for the post, as we no longer need it now. | |
// wp_cache_delete( $p->ID, 'post_meta' ); | |
// clean_object_term_cache( $p->ID, $post_type ); | |
} | |
} | |
// Grab last modified date | |
$sql = $wpdb->prepare(" SELECT MAX(p.post_modified_gmt) AS lastmod | |
FROM $wpdb->posts AS p | |
WHERE post_status IN ('publish') AND post_type = %s ", $post_type ); | |
$mod = $wpdb->get_var( $sql ); | |
// Generate terms URLs | |
$practitioner_terms = get_terms( 'TAXONOMY', 'orderby=count&hide_empty=0' ); | |
if( !empty( $practitioner_terms ) ){ | |
$pri = 1; | |
$chf = 'weekly'; | |
foreach ($practitioner_terms as $key => $term ){ | |
$url = array(); | |
$url['loc'] = site_url().'/sample/'.$term->slug; | |
$url['pri'] = $pri; | |
$url['mod'] = $mod; | |
$url['chf'] = $chf; | |
$output .= $wpseo_sitemaps->sitemap_url( $url ); | |
} | |
} | |
// Generate permutation & combinations | |
if( ( !empty( $practitioner_terms) ) && ( ! empty($posts_array ) ) ){ | |
$pri = 1; | |
$chf = 'weekly'; | |
wp_reset_postdata(); | |
foreach ($practitioner_terms as $key => $term ){ | |
$args = array( | |
'posts_per_page' => -1, | |
'orderby' => 'post_date', | |
'order' => 'DESC', | |
'post_type' => $post_type, | |
'post_status' => 'publish', | |
'suppress_filters' => true, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'TAXONOMY', | |
'field' => 'slug', | |
'terms' => $term->slug, | |
'operator' => 'IN' | |
), | |
), | |
); | |
$posts_array = get_posts( $args ); | |
//echo '<pre>';print_r($posts_array);echo '</pre>'; | |
$url = array(); | |
foreach ($posts_array as $key => $p ){ | |
$url['loc'] = site_url().'/sample/'.$term->slug.'/'.$p->post_name; | |
$url['pri'] = $pri; | |
$url['mod'] = $mod; | |
$url['chf'] = $chf; | |
$output .= $wpseo_sitemaps->sitemap_url( $url ); | |
} | |
} | |
} | |
if ( empty( $output ) ) { | |
$wpseo_sitemaps->bad_sitemap = true; | |
return; | |
} | |
//Build the full sitemap | |
$sitemap = '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; | |
$sitemap .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" '; | |
$sitemap .= 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n"; | |
$sitemap .= $output . '</urlset>'; | |
//echo $sitemap; | |
$wpseo_sitemaps->set_sitemap($sitemap); | |
} | |
/********************************************************* | |
* OR we can use $wpseo_sitemaps->register_sitemap( 'CUSTOM_KEY', 'METHOD' ); | |
********************************************************/ | |
add_action( 'init', 'ex_register_my_new_sitemap', 99 ); | |
/** | |
* On init, run the function that will register our new sitemap as well | |
* as the function that will be used to generate the XML. This creates an | |
* action that we can hook into built around the new | |
* sitemap name - 'wp_seo_do_sitemap_my_new_sitemap' | |
*/ | |
function ex_register_my_new_sitemap() { | |
global $wpseo_sitemaps; | |
$wpseo_sitemaps->register_sitemap( 'CUSTOM_KEY', 'ex_generate_origin_combo_sitemap' ); | |
} | |
add_action( 'init', 'init_do_sitemap_actions' ); | |
function init_do_sitemap_actions(){ | |
add_action( 'wp_seo_do_sitemap_our-CUSTOM_KEY', 'ex_generate_origin_combo_sitemap' ); | |
} | |
This comment has been minimized.
This comment has been minimized.
Where does this file want placing? Put this in the / within the plugin but sitemap and admin are for the plugin appear as before. https://s29.postimg.org/b34i2fzt3/customsitemap.jpg |
This comment has been minimized.
This comment has been minimized.
Thank you! It was a great help with my project! |
This comment has been minimized.
This comment has been minimized.
Where to put this file? Please guide me further. |
This comment has been minimized.
This comment has been minimized.
How do you use this? I've been struggling for hours trying to add my own custom sitemap url to yoast 5.0 's sitemap_index.xml. I will not add any new ones. I tried your post as well as this other post https://kb.yoast.com/kb/add-external-sitemap-to-index/ , added to my child theme's function.php, even tried adding to my main functions.php. It will NOT add custom sitemaps. What am I missing? Am missing a add_action command?? |
This comment has been minimized.
This comment has been minimized.
After searching for a while, I found in the Yoast docs that sitemaps are cached and needed to be disabled, then reenabled for these entries to actually show up in the generated sitemaps. |
This comment has been minimized.
This comment has been minimized.
Line 161 will cause a deprecation warning: To fix this, change line 161 to:
|
This comment has been minimized.
This comment has been minimized.
Hello, I get the following error: The following file is blocking your XML sitemaps from working properly. Either delete it (this can be done with the "Fix it" button) or disable Yoast SEO XML sitemaps. any suggestions? thks in advance |
This comment has been minimized.
This comment has been minimized.
With an updated version of YOAST 7.0.3 .., the entries per page option is not working at all .. any way around it?
|
This comment has been minimized.
This comment has been minimized.
thanks for that @haroldkyle |
This comment has been minimized.
This comment has been minimized.
Thanks for this post and the comments. I have made an adjusted version of this implementation that allows you to add yoast sitemaps with custom Pods / Pods Pages. |
This comment has been minimized.
This comment has been minimized.
In the filter, you want to "add" instead of overwriting right now. Otherwise if you do this multiple times, you'll just have the latest sitemap function that ran. So you'd want these changes to add multiple sitemap indexes : -function ex_add_sitemap_custom_items(){
+function ex_add_sitemap_custom_items($smp){
global $wpseo_sitemaps;
$date = $wpseo_sitemaps->get_last_modified('CUSTOM_POST_TYPE');
- $smp ='';
$smp .= '<sitemap>' . "\n"; |
This comment has been minimized.
This comment has been minimized.
Why is |
This comment has been minimized.
This comment has been minimized.
Thanks for the snippet. Modifying it a bit to adapt it to the latest Yoast version works like a charm. :) |
This comment has been minimized.
This comment has been minimized.
Yes - only Just added two new gists as a more comprehensive examples. All in one file, including writing sitemap Sitemap without a data source specified: Sitemap sourced from post type(s): |
This comment has been minimized.
It worked perfectly, thank u. :D