Skip to content

Commit

Permalink
Remove extra WP Query
Browse files Browse the repository at this point in the history
Passing the post_id to get_post is performs better than a fresh WP_Query.
  • Loading branch information
kraftbj authored Apr 20, 2018
2 parents cc415d3 + af8cfac commit d4b04cc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
15 changes: 3 additions & 12 deletions msm-sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,16 +447,6 @@ public static function generate_sitemap_for_date( $sitemap_date ) {
return;
}

// We need to get a WP_Query object for back-compat as we run a Loop when building
$query = new WP_Query( array(
'post__in' => $post_ids,
'post_type' => self::get_supported_post_types(),
'no_found_rows' => true,
'posts_per_page' => $per_page,
'ignore_sticky_posts' => true,
'post_status' => 'publish',
) );

$total_url_count = self::get_total_indexed_url_count();

// For migration: in case the previous version used an array for this option
Expand Down Expand Up @@ -484,8 +474,9 @@ public static function generate_sitemap_for_date( $sitemap_date ) {
$xml = new SimpleXMLElement( $namespace_str );

$url_count = 0;
while ( $query->have_posts() ) {
$query->the_post();
foreach ( $post_ids as $post_id ) {
$GLOBALS['post'] = get_post( $post_id );
setup_postdata( $GLOBALS['post'] );

if ( apply_filters( 'msm_sitemap_skip_post', false ) )
continue;
Expand Down
2 changes: 2 additions & 0 deletions tests/test-sitemap-creation.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function test_sitemap_posts_were_created() {
$this->assertNotEmpty( $xml_struct->url );
$this->assertNotEmpty( $xml_struct->url->loc );
$this->assertNotEmpty( $xml_struct->url->lastmod );
$this->assertNotEmpty( $xml_struct->url->changefreq );
$this->assertNotEmpty( $xml_struct->url->priority );
$this->assertContains( 'p=' . $post_id, (string) $xml_struct->url->loc );

$post = get_post( $post_id );
Expand Down

0 comments on commit d4b04cc

Please sign in to comment.