WP_Sitemaps_Posts::get_url_list( int $page_num, string $object_subtype =  ): array[]

In this article

Gets a URL list for a post type sitemap.

Parameters

$page_numintrequired
Page of results.
$object_subtypestringoptional
Post type name.

Default:''

Return

array[] Array of URL information for a sitemap.

Source

public function get_url_list( $page_num, $object_subtype = '' ) {
	// Restores the more descriptive, specific name for use within this method.
	$post_type = $object_subtype;

	// Bail early if the queried post type is not supported.
	$supported_types = $this->get_object_subtypes();

	if ( ! isset( $supported_types[ $post_type ] ) ) {
		return array();
	}

	/**
	 * Filters the posts URL list before it is generated.
	 *
	 * Returning a non-null value will effectively short-circuit the generation,
	 * returning that value instead.
	 *
	 * @since 5.5.0
	 *
	 * @param array[]|null $url_list  The URL list. Default null.
	 * @param string       $post_type Post type name.
	 * @param int          $page_num  Page of results.
	 */
	$url_list = apply_filters(
		'wp_sitemaps_posts_pre_url_list',
		null,
		$post_type,
		$page_num
	);

	if ( null !== $url_list ) {
		return $url_list;
	}

	$args          = $this->get_posts_query_args( $post_type );
	$args['paged'] = $page_num;

	$query = new WP_Query( $args );

	$url_list = array();

	/*
	 * Add a URL for the homepage in the pages sitemap.
	 * Shows only on the first page if the reading settings are set to display latest posts.
	 */
	if ( 'page' === $post_type && 1 === $page_num && 'posts' === get_option( 'show_on_front' ) ) {
		// Extract the data needed for home URL to add to the array.
		$sitemap_entry = array(
			'loc' => home_url( '/' ),
		);

		/*
		 * Get the most recent posts displayed on the homepage,
		 * and then sort them by their modified date to find
		 * the date the homepage was approximately last updated.
		 */
		$latest_posts = new WP_Query(
			array(
				'post_type'              => 'post',
				'post_status'            => 'publish',
				'orderby'                => 'date',
				'order'                  => 'DESC',
				'no_found_rows'          => true,
				'update_post_meta_cache' => false,
				'update_post_term_cache' => false,
			)
		);

		if ( ! empty( $latest_posts->posts ) ) {
			$posts = wp_list_sort( $latest_posts->posts, 'post_modified_gmt', 'DESC' );

			$sitemap_entry['lastmod'] = wp_date( DATE_W3C, strtotime( $posts[0]->post_modified_gmt ) );
		}

		/**
		 * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'.
		 *
		 * @since 5.5.0
		 *
		 * @param array $sitemap_entry Sitemap entry for the home page.
		 */
		$sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry );
		$url_list[]    = $sitemap_entry;
	}

	foreach ( $query->posts as $post ) {
		$sitemap_entry = array(
			'loc'     => get_permalink( $post ),
			'lastmod' => wp_date( DATE_W3C, strtotime( $post->post_modified_gmt ) ),
		);

		/**
		 * Filters the sitemap entry for an individual post.
		 *
		 * @since 5.5.0
		 *
		 * @param array   $sitemap_entry Sitemap entry for the post.
		 * @param WP_Post $post          Post object.
		 * @param string  $post_type     Name of the post_type.
		 */
		$sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type );
		$url_list[]    = $sitemap_entry;
	}

	return $url_list;
}

Hooks

apply_filters( ‘wp_sitemaps_posts_entry’, array $sitemap_entry, WP_Post $post, string $post_type )

Filters the sitemap entry for an individual post.

apply_filters( ‘wp_sitemaps_posts_pre_url_list’, array[]|null $url_list, string $post_type, int $page_num )

Filters the posts URL list before it is generated.

apply_filters( ‘wp_sitemaps_posts_show_on_front_entry’, array $sitemap_entry )

Filters the sitemap entry for the home page when the ‘show_on_front’ option equals ‘posts’.

Changelog

VersionDescription
5.9.0Renamed $post_type to $object_subtype to match parent class for PHP 8 named parameter support.
5.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.