sinanisler logo

WordPress Built in SEO Sitemap.xml, No Plugin Sitemap.xml

 

add this code to the functions.php and you will have sitemap.xml up and running.

it is not very advance like pagination or splitting so if the site is bigger than 3k page/post I don’t recommend this method. 🙂

 

 

 

function generate_sitemap() {
    $output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    $output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

    // Add posts to the sitemap
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => -1,
    );
    $posts = new WP_Query( $args );
    if ( $posts->have_posts() ) {
        while ( $posts->have_posts() ) {
            $posts->the_post();
            $post_permalink = get_permalink();
            $post_modified  = get_the_modified_time( 'Y-m-d' );

            $output .= "\t<url>\n";
            $output .= "\t\t<loc>" . esc_url( $post_permalink ) . "</loc>\n";
            $output .= "\t\t<lastmod>" . esc_html( $post_modified ) . "</lastmod>\n";
            $output .= "\t</url>\n";
        }
    }

    // Add pages to the sitemap
    $args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,
    );
    $pages = new WP_Query( $args );
    if ( $pages->have_posts() ) {
        while ( $pages->have_posts() ) {
            $pages->the_post();
            $page_permalink = get_permalink();
            $page_modified  = get_the_modified_time( 'Y-m-d' );

            $output .= "\t<url>\n";
            $output .= "\t\t<loc>" . esc_url( $page_permalink ) . "</loc>\n";
            $output .= "\t\t<lastmod>" . esc_html( $page_modified ) . "</lastmod>\n";
            $output .= "\t</url>\n";
        }
    }

    // Add custom post types to the sitemap (if any)
    $custom_post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' );
    if ( $custom_post_types ) {
        foreach ( $custom_post_types as $post_type ) {
            $args = array(
                'post_type'      => $post_type->name,
                'posts_per_page' => -1,
            );
            $custom_posts = new WP_Query( $args );
            if ( $custom_posts->have_posts() ) {
                while ( $custom_posts->have_posts() ) {
                    $custom_posts->the_post();
                    $custom_post_permalink = get_permalink();
                    $custom_post_modified  = get_the_modified_time( 'Y-m-d' );

                    $output .= "\t<url>\n";
                    $output .= "\t\t<loc>" . esc_url( $custom_post_permalink ) . "</loc>\n";
                    $output .= "\t\t<lastmod>" . esc_html( $custom_post_modified ) . "</lastmod>\n";
                    $output .= "\t</url>\n";
                }
            }
        }
    }

    $output .= '</urlset>';

    // Output the sitemap.xml content
    $sitemap_file = fopen( ABSPATH . 'sitemap.xml', 'w' );
    fwrite( $sitemap_file, $output );
    fclose( $sitemap_file );
}

add_action( 'init', 'generate_sitemap' );

 

Leave the first comment