Skip to content

Commit

Permalink
Merge branch 'fix/index-query' into develop-built
Browse files Browse the repository at this point in the history
  • Loading branch information
claimableperch committed Jul 10, 2024
2 parents 2ceec77 + 25e6604 commit a42635f
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions msm-sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,22 +724,7 @@ public static function build_root_sitemap_xml( $year = false ) {
}
$sitemaps = $wpdb->get_col( $query );
} else {
$args = [
'post_type' => Metro_Sitemap::SITEMAP_CPT,
'orderby' => 'post_date',
'order' => 'DESC',
'numberposts' => self::max_sitemap_length(),
];
if ( is_numeric( $year ) ) {
$args['m'] = $year;
}

$sitemaps = array_map(
function ( \WP_Post $post ): string {
return $post->post_date;
},
get_posts( $args )
);
$sitemaps = self::get_sitemap_dates( $year );
}
// Sometimes duplicate sitemaps exist, lets make sure so they are not output
$sitemaps = array_unique( $sitemaps );
Expand Down Expand Up @@ -767,6 +752,35 @@ function ( \WP_Post $post ): string {
return $xml->asXML();
}

/**
* Return sitemap post_dates.
*
* @param ?int $year year to list.
*
* @return string[] dates of sitemap posts.
*/
public static function get_sitemap_dates( $year = false ): array {
$args = [
'post_type' => Metro_Sitemap::SITEMAP_CPT,
'orderby' => 'post_date',
'order' => 'DESC',
'fields' => 'ids',
'numberposts' => self::max_sitemap_length(),
];
if ( is_numeric( $year ) ) {
$args['m'] = $year;
}

$sitemaps = array_map(
function ( int $post_id ): string {
return get_post( $post_id )->post_date;
},
get_posts( $args )
);

return $sitemaps;
}

/**
* Build the sitemap URL for a given date
* @param string $sitemap_date
Expand Down

0 comments on commit a42635f

Please sign in to comment.