WP_Sitemaps_Users
云策文档标注
概述
WP_Sitemaps_Users 是 WordPress 5.5.0 引入的 XML 站点地图提供者类,专门用于生成用户(作者)的站点地图。它继承自 WP_Sitemaps_Provider,通过 WP_User_Query 检索用户数据并生成 URL 列表。
关键要点
- WP_Sitemaps_Users 扩展自 WP_Sitemaps_Provider,提供用户站点地图功能。
- 核心方法包括 get_url_list 用于获取用户 URL 列表,get_max_num_pages 用于计算最大页数,get_users_query_args 用于定义用户查询参数。
- 支持过滤器钩子如 wp_sitemaps_users_pre_url_list、wp_sitemaps_users_entry 和 wp_sitemaps_users_query_args,允许开发者自定义站点地图生成过程。
- 默认排除附件和页面的作者页面,仅包含有公开文章的用户。
代码示例
// 示例:使用 WP_Sitemaps_Users 获取用户站点地图 URL 列表
$sitemap_provider = new WP_Sitemaps_Users();
$url_list = $sitemap_provider->get_url_list(1); // 获取第一页的用户 URL 列表
print_r($url_list);注意事项
- WP_Sitemaps_Users 自 WordPress 5.5.0 起可用,使用时需确保 WordPress 版本兼容。
- get_users_query_args 方法受 wp_sitemaps_users_query_args 过滤器影响,可修改查询参数以调整用户筛选条件。
- 站点地图 URL 数量受 wp_sitemaps_get_max_urls 函数控制,需注意分页逻辑。
原文内容
Users XML sitemap provider.
Methods
| Name | Description |
|---|---|
| WP_Sitemaps_Users::__construct | WP_Sitemaps_Users constructor. |
| WP_Sitemaps_Users::get_max_num_pages | Gets the max number of pages available for the object type. |
| WP_Sitemaps_Users::get_url_list | Gets a URL list for a user sitemap. |
| WP_Sitemaps_Users::get_users_query_args | Returns the query args for retrieving users to list in the sitemap. |
Source
class WP_Sitemaps_Users extends WP_Sitemaps_Provider {
/**
* WP_Sitemaps_Users constructor.
*
* @since 5.5.0
*/
public function __construct() {
$this->name = 'users';
$this->object_type = 'user';
}
/**
* Gets a URL list for a user sitemap.
*
* @since 5.5.0
*
* @param int $page_num Page of results.
* @param string $object_subtype Optional. Not applicable for Users but
* required for compatibility with the parent
* provider class. Default empty.
* @return array[] Array of URL information for a sitemap.
*/
public function get_url_list( $page_num, $object_subtype = '' ) {
/**
* Filters the users 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 int $page_num Page of results.
*/
$url_list = apply_filters(
'wp_sitemaps_users_pre_url_list',
null,
$page_num
);
if ( null !== $url_list ) {
return $url_list;
}
$args = $this->get_users_query_args();
$args['paged'] = $page_num;
$query = new WP_User_Query( $args );
$users = $query->get_results();
$url_list = array();
foreach ( $users as $user ) {
$sitemap_entry = array(
'loc' => get_author_posts_url( $user->ID ),
);
/**
* Filters the sitemap entry for an individual user.
*
* @since 5.5.0
*
* @param array $sitemap_entry Sitemap entry for the user.
* @param WP_User $user User object.
*/
$sitemap_entry = apply_filters( 'wp_sitemaps_users_entry', $sitemap_entry, $user );
$url_list[] = $sitemap_entry;
}
return $url_list;
}
/**
* Gets the max number of pages available for the object type.
*
* @since 5.5.0
*
* @see WP_Sitemaps_Provider::max_num_pages
*
* @param string $object_subtype Optional. Not applicable for Users but
* required for compatibility with the parent
* provider class. Default empty.
* @return int Total page count.
*/
public function get_max_num_pages( $object_subtype = '' ) {
/**
* Filters the max number of pages for a user sitemap before it is generated.
*
* Returning a non-null value will effectively short-circuit the generation,
* returning that value instead.
*
* @since 5.5.0
*
* @param int|null $max_num_pages The maximum number of pages. Default null.
*/
$max_num_pages = apply_filters( 'wp_sitemaps_users_pre_max_num_pages', null );
if ( null !== $max_num_pages ) {
return $max_num_pages;
}
$args = $this->get_users_query_args();
$query = new WP_User_Query( $args );
$total_users = $query->get_total();
return (int) ceil( $total_users / wp_sitemaps_get_max_urls( $this->object_type ) );
}
/**
* Returns the query args for retrieving users to list in the sitemap.
*
* @since 5.5.0
*
* @return array Array of WP_User_Query arguments.
*/
protected function get_users_query_args() {
$public_post_types = get_post_types(
array(
'public' => true,
)
);
// We're not supporting sitemaps for author pages for attachments and pages.
unset( $public_post_types['attachment'] );
unset( $public_post_types['page'] );
/**
* Filters the query arguments for authors with public posts.
*
* Allows modification of the authors query arguments before querying.
*
* @see WP_User_Query for a full list of arguments
*
* @since 5.5.0
*
* @param array $args Array of WP_User_Query arguments.
*/
$args = apply_filters(
'wp_sitemaps_users_query_args',
array(
'has_published_posts' => array_keys( $public_post_types ),
'number' => wp_sitemaps_get_max_urls( $this->object_type ),
)
);
return $args;
}
}
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |