author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
16 | 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 |
|
19 | 56 |
* @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class |
57 |
* for PHP 8 named parameter support. |
|
16 | 58 |
* |
19 | 59 |
* @param int $page_num Page of results. |
60 |
* @param string $object_subtype Optional. Post type name. Default empty. |
|
61 |
* |
|
62 |
* @return array[] Array of URL information for a sitemap. |
|
16 | 63 |
*/ |
19 | 64 |
public function get_url_list( $page_num, $object_subtype = '' ) { |
65 |
// Restores the more descriptive, specific name for use within this method. |
|
66 |
$post_type = $object_subtype; |
|
67 |
||
16 | 68 |
// Bail early if the queried post type is not supported. |
69 |
$supported_types = $this->get_object_subtypes(); |
|
70 |
||
71 |
if ( ! isset( $supported_types[ $post_type ] ) ) { |
|
72 |
return array(); |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Filters the posts URL list before it is generated. |
|
77 |
* |
|
19 | 78 |
* Returning a non-null value will effectively short-circuit the generation, |
16 | 79 |
* returning that value instead. |
80 |
* |
|
81 |
* @since 5.5.0 |
|
82 |
* |
|
19 | 83 |
* @param array[]|null $url_list The URL list. Default null. |
84 |
* @param string $post_type Post type name. |
|
85 |
* @param int $page_num Page of results. |
|
16 | 86 |
*/ |
87 |
$url_list = apply_filters( |
|
88 |
'wp_sitemaps_posts_pre_url_list', |
|
89 |
null, |
|
90 |
$post_type, |
|
91 |
$page_num |
|
92 |
); |
|
93 |
||
94 |
if ( null !== $url_list ) { |
|
95 |
return $url_list; |
|
96 |
} |
|
97 |
||
98 |
$args = $this->get_posts_query_args( $post_type ); |
|
99 |
$args['paged'] = $page_num; |
|
100 |
||
101 |
$query = new WP_Query( $args ); |
|
102 |
||
103 |
$url_list = array(); |
|
104 |
||
105 |
/* |
|
106 |
* Add a URL for the homepage in the pages sitemap. |
|
107 |
* Shows only on the first page if the reading settings are set to display latest posts. |
|
108 |
*/ |
|
109 |
if ( 'page' === $post_type && 1 === $page_num && 'posts' === get_option( 'show_on_front' ) ) { |
|
110 |
// Extract the data needed for home URL to add to the array. |
|
111 |
$sitemap_entry = array( |
|
112 |
'loc' => home_url( '/' ), |
|
113 |
); |
|
114 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
* Get the most recent posts displayed on the homepage, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
* and then sort them by their modified date to find |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
* the date the homepage was approximately last updated. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
119 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
$latest_posts = new WP_Query( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
121 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
122 |
'post_type' => 'post', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
123 |
'post_status' => 'publish', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
124 |
'orderby' => 'date', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
125 |
'order' => 'DESC', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
126 |
'no_found_rows' => true, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
127 |
'update_post_meta_cache' => false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
128 |
'update_post_term_cache' => false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
129 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
if ( ! empty( $latest_posts->posts ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
$posts = wp_list_sort( $latest_posts->posts, 'post_modified_gmt', 'DESC' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
$sitemap_entry['lastmod'] = wp_date( DATE_W3C, strtotime( $posts[0]->post_modified_gmt ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
|
16 | 138 |
/** |
139 |
* Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'. |
|
140 |
* |
|
141 |
* @since 5.5.0 |
|
142 |
* |
|
143 |
* @param array $sitemap_entry Sitemap entry for the home page. |
|
144 |
*/ |
|
145 |
$sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry ); |
|
146 |
$url_list[] = $sitemap_entry; |
|
147 |
} |
|
148 |
||
149 |
foreach ( $query->posts as $post ) { |
|
150 |
$sitemap_entry = array( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
'loc' => get_permalink( $post ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
152 |
'lastmod' => wp_date( DATE_W3C, strtotime( $post->post_modified_gmt ) ), |
16 | 153 |
); |
154 |
||
155 |
/** |
|
156 |
* Filters the sitemap entry for an individual post. |
|
157 |
* |
|
158 |
* @since 5.5.0 |
|
159 |
* |
|
160 |
* @param array $sitemap_entry Sitemap entry for the post. |
|
161 |
* @param WP_Post $post Post object. |
|
162 |
* @param string $post_type Name of the post_type. |
|
163 |
*/ |
|
164 |
$sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type ); |
|
165 |
$url_list[] = $sitemap_entry; |
|
166 |
} |
|
167 |
||
168 |
return $url_list; |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Gets the max number of pages available for the object type. |
|
173 |
* |
|
174 |
* @since 5.5.0 |
|
19 | 175 |
* @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class |
176 |
* for PHP 8 named parameter support. |
|
16 | 177 |
* |
19 | 178 |
* @param string $object_subtype Optional. Post type name. Default empty. |
16 | 179 |
* @return int Total number of pages. |
180 |
*/ |
|
19 | 181 |
public function get_max_num_pages( $object_subtype = '' ) { |
182 |
if ( empty( $object_subtype ) ) { |
|
16 | 183 |
return 0; |
184 |
} |
|
185 |
||
19 | 186 |
// Restores the more descriptive, specific name for use within this method. |
187 |
$post_type = $object_subtype; |
|
188 |
||
16 | 189 |
/** |
190 |
* Filters the max number of pages before it is generated. |
|
191 |
* |
|
192 |
* Passing a non-null value will short-circuit the generation, |
|
193 |
* returning that value instead. |
|
194 |
* |
|
195 |
* @since 5.5.0 |
|
196 |
* |
|
197 |
* @param int|null $max_num_pages The maximum number of pages. Default null. |
|
198 |
* @param string $post_type Post type name. |
|
199 |
*/ |
|
200 |
$max_num_pages = apply_filters( 'wp_sitemaps_posts_pre_max_num_pages', null, $post_type ); |
|
201 |
||
202 |
if ( null !== $max_num_pages ) { |
|
203 |
return $max_num_pages; |
|
204 |
} |
|
205 |
||
206 |
$args = $this->get_posts_query_args( $post_type ); |
|
207 |
$args['fields'] = 'ids'; |
|
208 |
$args['no_found_rows'] = false; |
|
209 |
||
210 |
$query = new WP_Query( $args ); |
|
211 |
||
212 |
$min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0; |
|
213 |
return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1; |
|
214 |
} |
|
215 |
||
216 |
/** |
|
217 |
* Returns the query args for retrieving posts to list in the sitemap. |
|
218 |
* |
|
219 |
* @since 5.5.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
220 |
* @since 6.1.0 Added `ignore_sticky_posts` default parameter. |
16 | 221 |
* |
222 |
* @param string $post_type Post type name. |
|
223 |
* @return array Array of WP_Query arguments. |
|
224 |
*/ |
|
225 |
protected function get_posts_query_args( $post_type ) { |
|
226 |
/** |
|
227 |
* Filters the query arguments for post type sitemap queries. |
|
228 |
* |
|
229 |
* @see WP_Query for a full list of arguments. |
|
230 |
* |
|
231 |
* @since 5.5.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
232 |
* @since 6.1.0 Added `ignore_sticky_posts` default parameter. |
16 | 233 |
* |
234 |
* @param array $args Array of WP_Query arguments. |
|
235 |
* @param string $post_type Post type name. |
|
236 |
*/ |
|
237 |
$args = apply_filters( |
|
238 |
'wp_sitemaps_posts_query_args', |
|
239 |
array( |
|
240 |
'orderby' => 'ID', |
|
241 |
'order' => 'ASC', |
|
242 |
'post_type' => $post_type, |
|
243 |
'posts_per_page' => wp_sitemaps_get_max_urls( $this->object_type ), |
|
244 |
'post_status' => array( 'publish' ), |
|
245 |
'no_found_rows' => true, |
|
246 |
'update_post_term_cache' => false, |
|
247 |
'update_post_meta_cache' => false, |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
248 |
'ignore_sticky_posts' => true, // Sticky posts will still appear, but they won't be moved to the front. |
16 | 249 |
), |
250 |
$post_type |
|
251 |
); |
|
252 |
||
253 |
return $args; |
|
254 |
} |
|
255 |
} |