wp_sitemaps_posts_query_args
云策文档标注
概述
wp_sitemaps_posts_query_args 是一个 WordPress 过滤器,用于修改文章类型站点地图查询的 WP_Query 参数。它允许开发者自定义查询条件,以控制哪些文章被包含在站点地图中。
关键要点
- 过滤器名称:wp_sitemaps_posts_query_args
- 参数:$args(WP_Query 参数数组)和 $post_type(文章类型名称)
- 默认查询参数包括按 ID 升序排序、仅发布状态、忽略置顶文章等
- 可用于添加自定义查询逻辑,如 meta_query
- 引入版本:WordPress 5.5.0,6.1.0 版本添加了 ignore_sticky_posts 默认参数
代码示例
add_filter( 'wp_sitemaps_posts_query_args', 'wpdocs_sitemaps_posts_query_args_callback', 10, 2 );
function wpdocs_sitemaps_posts_query_args_callback( $args, $post_type ) {
$post_type = 'post_type_name'; // 你的文章类型
$args = array(
'post_type' => $post_type,
'order' => 'desc',
'post_status' => array( 'publish' ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'some_key_name',
'value' => 'some_value',
'compare' => 'LIKE',
),
array(
'key' => 'some_key_name_2',
'value' => 'some_value_2',
'compare' => '>=',
),
),
);
return $args;
}注意事项
- 参考 WP_Query 获取完整的参数列表
- 确保返回的 $args 数组包含必要的查询条件,以避免站点地图生成错误
- 此过滤器在 WP_Sitemaps_Posts::get_posts_query_args() 方法中使用
原文内容
Filters the query arguments for post type sitemap queries.
Description
See also
- WP_Query: for a full list of arguments.
Parameters
$argsarray-
Array of WP_Query arguments.
$post_typestring-
Post type name.
Source
$args = apply_filters(
'wp_sitemaps_posts_query_args',
array(
'orderby' => 'ID',
'order' => 'ASC',
'post_type' => $post_type,
'posts_per_page' => wp_sitemaps_get_max_urls( $this->object_type ),
'post_status' => array( 'publish' ),
'no_found_rows' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'ignore_sticky_posts' => true, // Sticky posts will still appear, but they won't be moved to the front.
),
$post_type
);
Skip to note 2 content
Krunal Bhimajiyani
Filters the custom query arguments for post-type sitemap queries.
add_filter( 'wp_sitemaps_posts_query_args', 'wpdocs_sitemaps_posts_query_args_callback', 10, 2 ); function wpdocs_sitemaps_posts_query_args_callback( $args, $post_type ) { $post_type = 'post_type_name'; // your post type $args = array( 'post_type' => $post_type, 'order' => 'desc', 'post_status' => array( 'publish' ), 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'some_key_name', 'value' => 'some_value', 'compare' => 'LIKE', ), array( 'key' => 'some_key_name_2', 'value' => 'some_value_2', 'compare' => '>=', ), ), ); return $args; }