Skip to content

Instantly share code, notes, and snippets.

@dewebco
Created October 29, 2019 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dewebco/5f5cbf7411d893e168769bd94fd247fa to your computer and use it in GitHub Desktop.
Save dewebco/5f5cbf7411d893e168769bd94fd247fa to your computer and use it in GitHub Desktop.
add_action("publish_post", "create_sitemap"); add_action("publish_page", "create_sitemap"); function create_sitemap() { $postsForSitemap = get_posts( array( 'numberposts' => -1, 'orderby' => 'modified', 'post_type' => array( 'post', 'page' ), 'order' => 'DESC' ) ); $sitemap = '<?xml version="1.0" encoding="UTF-8"?>'; $sitemap .= "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n"; foreach( $postsForSitemap as $post ) { setup_postdata( $post ); $postdate = explode( " ", $post->post_modified ); $sitemap .= "\t" . '<url>' . "\n" . "\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' . "\n\t\t" . '<lastmod>' . $postdate[0] . '</lastmod>' . "\n\t\t" . '<changefreq>monthly</changefreq>' . "\n\t" . '</url>' . "\n"; } $sitemap .= '</urlset>'; $fp = fopen( ABSPATH . "sitemap.xml", 'w' ); fwrite( $fp, $sitemap ); fclose( $fp ); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment