Skip to content

Instantly share code, notes, and snippets.

Last active March 28, 2024 05:19
Show Gist options
  • Save leepowers/2b32f734571cbeadc811b93501cfcced to your computer and use it in GitHub Desktop.
Save leepowers/2b32f734571cbeadc811b93501cfcced to your computer and use it in GitHub Desktop.
WordPress Yoast SEO: Create a custom sitemap with data not sourced from a custom post type.
* - Search and replace the `CUSTOM_SITEMAP` string with a lowercase identifier name: e.g., "vehicles", "customer_profiles"
* - The `your_alternate_data_source()` function does not exist; it's simply a placeholder for your own function that returns some sort of data array.
* - Uses heredocs for inline XML:
* Uncomment the next line of code to disable sitemap caching.
* - For development environments and debugging only.
* - All other scenarios, follow the "Manual Sitemap Update" instructions in the Yoast documentation:
* -
#add_filter("wpseo_enable_xml_sitemap_transient_caching", "__return_false");
* Add CUSTOM_SITEMAP-sitemap.xml to Yoast sitemap index
function CUSTOM_SITEMAP_sitemap_index($sitemap_index) {
global $wpseo_sitemaps;
$sitemap_url = home_url("CUSTOM_SITEMAP-sitemap.xml");
$sitemap_date = date(DATE_W3C); # Current date and time in sitemap format.
$custom_sitemap = <<<SITEMAP_INDEX_ENTRY
$sitemap_index .= sprintf($custom_sitemap, $sitemap_url, $sitemap_date);
return $sitemap_index;
add_filter("wpseo_sitemap_index", "CUSTOM_SITEMAP_sitemap_index");
* Register CUSTOM_SITEMAP sitemap with Yoast
function CUSTOM_SITEMAP_sitemap_register() {
global $wpseo_sitemaps;
if (isset($wpseo_sitemaps) && !empty($wpseo_sitemaps)) {
$wpseo_sitemaps->register_sitemap("CUSTOM_SITEMAP", "CUSTOM_SITEMAP_sitemap_generate");
add_action("init", "CUSTOM_SITEMAP_sitemap_register");
* Generate CUSTOM_SITEMAP sitemap XML body
function CUSTOM_SITEMAP_sitemap_generate() {
global $wpseo_sitemaps;
$data = your_alternate_data_source(); # Replace this with your own data source function
$urls = array();
foreach ($data as $item) {
$urls[]= $wpseo_sitemaps->renderer->sitemap_url(array(
"mod" => $item->date, # <lastmod></lastmod>
"loc" => $item->url, # <loc></loc>
"images" => array(
array( # <image:image></image:image>
"src" => $item->image_url, # <image:loc></image:loc>
"title" => $item->image_title, # <image:title></image:title>
"alt" => $item->image_title, # <image:caption></image:caption>
$sitemap_body = <<<SITEMAP_BODY
$sitemap = sprintf($sitemap_body, implode("\n", $urls));
Copy link

Hi how to add double image ? E.g product variants have photo ids like 11111,22222,33333 in the 'product_image_galery' meta. How can I add?

Copy link

abhiXweb commented Feb 15, 2023

If you're talking about Product Gallery Images, then you can achieve this by:

$attach_ids= "11111,22222,33333";
update_post_meta( $product_id, '_product_image_gallery', $attach_ids);

NOTE: The $product_id would be your parent product id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment