[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/sitemaps/providers/ -> class-wp-sitemaps-posts.php (source)

   1  <?php
   2  /**
   3   * Sitemaps: WP_Sitemaps_Posts class
   4   *
   5   * Builds the sitemaps for the 'post' object type.
   6   *
   7   * @package WordPress
   8   * @subpackage Sitemaps
   9   * @since 5.5.0
  10   */
  11  
  12  /**
  13   * Posts XML sitemap provider.
  14   *
  15   * @since 5.5.0
  16   */
  17  class WP_Sitemaps_Posts extends WP_Sitemaps_Provider {
  18      /**
  19       * WP_Sitemaps_Posts constructor.
  20       *
  21       * @since 5.5.0
  22       */
  23  	public function __construct() {
  24          $this->name        = 'posts';
  25          $this->object_type = 'post';
  26      }
  27  
  28      /**
  29       * Returns the public post types, which excludes nav_items and similar types.
  30       * Attachments are also excluded. This includes custom post types with public = true.
  31       *
  32       * @since 5.5.0
  33       *
  34       * @return WP_Post_Type[] Array of registered post type objects keyed by their name.
  35       */
  36  	public function get_object_subtypes() {
  37          $post_types = get_post_types( array( 'public' => true ), 'objects' );
  38          unset( $post_types['attachment'] );
  39  
  40          $post_types = array_filter( $post_types, 'is_post_type_viewable' );
  41  
  42          /**
  43           * Filters the list of post object sub types available within the sitemap.
  44           *
  45           * @since 5.5.0
  46           *
  47           * @param WP_Post_Type[] $post_types Array of registered post type objects keyed by their name.
  48           */
  49          return apply_filters( 'wp_sitemaps_post_types', $post_types );
  50      }
  51  
  52      /**
  53       * Gets a URL list for a post type sitemap.
  54       *
  55       * @since 5.5.0
  56       * @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class
  57       *              for PHP 8 named parameter support.
  58       *
  59       * @param int    $page_num       Page of results.
  60       * @param string $object_subtype Optional. Post type name. Default empty.
  61       * @return array[] Array of URL information for a sitemap.
  62       */
  63  	public function get_url_list( $page_num, $object_subtype = '' ) {
  64          // Restores the more descriptive, specific name for use within this method.
  65          $post_type = $object_subtype;
  66  
  67          // Bail early if the queried post type is not supported.
  68          $supported_types = $this->get_object_subtypes();
  69  
  70          if ( ! isset( $supported_types[ $post_type ] ) ) {
  71              return array();
  72          }
  73  
  74          /**
  75           * Filters the posts URL list before it is generated.
  76           *
  77           * Returning a non-null value will effectively short-circuit the generation,
  78           * returning that value instead.
  79           *
  80           * @since 5.5.0
  81           *
  82           * @param array[]|null $url_list  The URL list. Default null.
  83           * @param string       $post_type Post type name.
  84           * @param int          $page_num  Page of results.
  85           */
  86          $url_list = apply_filters(
  87              'wp_sitemaps_posts_pre_url_list',
  88              null,
  89              $post_type,
  90              $page_num
  91          );
  92  
  93          if ( null !== $url_list ) {
  94              return $url_list;
  95          }
  96  
  97          $args          = $this->get_posts_query_args( $post_type );
  98          $args['paged'] = $page_num;
  99  
 100          $query = new WP_Query( $args );
 101  
 102          $url_list = array();
 103  
 104          /*
 105           * Add a URL for the homepage in the pages sitemap.
 106           * Shows only on the first page if the reading settings are set to display latest posts.
 107           */
 108          if ( 'page' === $post_type && 1 === $page_num && 'posts' === get_option( 'show_on_front' ) ) {
 109              // Extract the data needed for home URL to add to the array.
 110              $sitemap_entry = array(
 111                  'loc' => home_url( '/' ),
 112              );
 113  
 114              /*
 115               * Get the most recent posts displayed on the homepage,
 116               * and then sort them by their modified date to find
 117               * the date the homepage was approximately last updated.
 118               */
 119              $latest_posts = new WP_Query(
 120                  array(
 121                      'post_type'              => 'post',
 122                      'post_status'            => 'publish',
 123                      'orderby'                => 'date',
 124                      'order'                  => 'DESC',
 125                      'no_found_rows'          => true,
 126                      'update_post_meta_cache' => false,
 127                      'update_post_term_cache' => false,
 128                  )
 129              );
 130  
 131              if ( ! empty( $latest_posts->posts ) ) {
 132                  $posts = wp_list_sort( $latest_posts->posts, 'post_modified_gmt', 'DESC' );
 133  
 134                  $sitemap_entry['lastmod'] = wp_date( DATE_W3C, strtotime( $posts[0]->post_modified_gmt ) );
 135              }
 136  
 137              /**
 138               * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'.
 139               *
 140               * @since 5.5.0
 141               *
 142               * @param array $sitemap_entry Sitemap entry for the home page.
 143               */
 144              $sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry );
 145              $url_list[]    = $sitemap_entry;
 146          }
 147  
 148          foreach ( $query->posts as $post ) {
 149              $sitemap_entry = array(
 150                  'loc'     => get_permalink( $post ),
 151                  'lastmod' => wp_date( DATE_W3C, strtotime( $post->post_modified_gmt ) ),
 152              );
 153  
 154              /**
 155               * Filters the sitemap entry for an individual post.
 156               *
 157               * @since 5.5.0
 158               *
 159               * @param array   $sitemap_entry Sitemap entry for the post.
 160               * @param WP_Post $post          Post object.
 161               * @param string  $post_type     Name of the post_type.
 162               */
 163              $sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type );
 164              $url_list[]    = $sitemap_entry;
 165          }
 166  
 167          return $url_list;
 168      }
 169  
 170      /**
 171       * Gets the max number of pages available for the object type.
 172       *
 173       * @since 5.5.0
 174       * @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class
 175       *              for PHP 8 named parameter support.
 176       *
 177       * @param string $object_subtype Optional. Post type name. Default empty.
 178       * @return int Total number of pages.
 179       */
 180  	public function get_max_num_pages( $object_subtype = '' ) {
 181          if ( empty( $object_subtype ) ) {
 182              return 0;
 183          }
 184  
 185          // Restores the more descriptive, specific name for use within this method.
 186          $post_type = $object_subtype;
 187  
 188          /**
 189           * Filters the max number of pages before it is generated.
 190           *
 191           * Passing a non-null value will short-circuit the generation,
 192           * returning that value instead.
 193           *
 194           * @since 5.5.0
 195           *
 196           * @param int|null $max_num_pages The maximum number of pages. Default null.
 197           * @param string   $post_type     Post type name.
 198           */
 199          $max_num_pages = apply_filters( 'wp_sitemaps_posts_pre_max_num_pages', null, $post_type );
 200  
 201          if ( null !== $max_num_pages ) {
 202              return $max_num_pages;
 203          }
 204  
 205          $args                  = $this->get_posts_query_args( $post_type );
 206          $args['fields']        = 'ids';
 207          $args['no_found_rows'] = false;
 208  
 209          $query = new WP_Query( $args );
 210  
 211          $min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0;
 212          return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1;
 213      }
 214  
 215      /**
 216       * Returns the query args for retrieving posts to list in the sitemap.
 217       *
 218       * @since 5.5.0
 219       * @since 6.1.0 Added `ignore_sticky_posts` default parameter.
 220       *
 221       * @param string $post_type Post type name.
 222       * @return array Array of WP_Query arguments.
 223       */
 224  	protected function get_posts_query_args( $post_type ) {
 225          /**
 226           * Filters the query arguments for post type sitemap queries.
 227           *
 228           * @see WP_Query for a full list of arguments.
 229           *
 230           * @since 5.5.0
 231           * @since 6.1.0 Added `ignore_sticky_posts` default parameter.
 232           *
 233           * @param array  $args      Array of WP_Query arguments.
 234           * @param string $post_type Post type name.
 235           */
 236          $args = apply_filters(
 237              'wp_sitemaps_posts_query_args',
 238              array(
 239                  'orderby'                => 'ID',
 240                  'order'                  => 'ASC',
 241                  'post_type'              => $post_type,
 242                  'posts_per_page'         => wp_sitemaps_get_max_urls( $this->object_type ),
 243                  'post_status'            => array( 'publish' ),
 244                  'no_found_rows'          => true,
 245                  'update_post_term_cache' => false,
 246                  'update_post_meta_cache' => false,
 247                  'ignore_sticky_posts'    => true, // Sticky posts will still appear, but they won't be moved to the front.
 248              ),
 249              $post_type
 250          );
 251  
 252          return $args;
 253      }
 254  }


Generated : Tue Aug 18 08:20:25 2026 Cross-referenced by PHPXref