wp_sitemaps_posts_query_args │ filter-hook │ WP 5.5.0

Allows you to change WP_Query parameters for the posts sitemap.

Usage

add_filter( 'wp_sitemaps_posts_query_args', 'wp_kama_sitemaps_posts_query_args_filter', 10, 2 );

/**
 * Function for `wp_sitemaps_posts_query_args` filter-hook.
 * 
 * @param array  $args      Array of WP_Query arguments.
 * @param string $post_type Post type name.
 *
 * @return array
 */
function wp_kama_sitemaps_posts_query_args_filter( $args, $post_type ){
	// filter...
	return $args;
}
$args(array)

An array of parameters passed to WP_Query when posts are retrieved for the sitemap.

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,
)
$post_type(string)
The post type for which query parameters are filtered.

Examples

#1 Exclude posts from the sitemap

Suppose you are developing a plugin that adds a noindex meta tag to some pages. It would also be useful to exclude these pages from the sitemap.

add_filter( 'wp_sitemaps_posts_query_args', 'kama_sitemaps_posts_query_args', 10, 2 );
function kama_sitemaps_posts_query_args( $args, $post_type ) {
	// Not our post type
	if ( 'post' !== $post_type ) {
		return $args;
	}

	// Account for this parameter already being set
	$args['post__not_in'] = isset( $args['post__not_in'] ) ? $args['post__not_in'] : [];

	// Exclude posts
	$args['post__not_in'][] = 12;
	$args['post__not_in'][] = 24;

	return $args;
}

This example can be adapted for dynamic filtering by meta-field:

add_filter( 'wp_sitemaps_posts_query_args', 'kama_sitemaps_posts_query_args', 10, 2 );
function kama_sitemaps_posts_query_args( $args, $post_type ) {

	// exclude posts with the noindex meta field set
	$args['meta_query'] = [
		[
			'key'         => 'noindex',
			'compare_key' => 'NOT EXISTS',
		],
	];

	return $args;
}

It is assumed that the meta-field may or may not exist: if it exists, then the post must be excluded from the index. This approach speeds up the query because you can use compare_key, which compares the meta key rather than the value, and therefore works using the MySQL index.

Changelog

Since 5.5.0 Introduced.
Since 6.1.0 Added ignore_sticky_posts default parameter.

Where the hook is called

WP_Sitemaps_Posts::get_posts_query_args()
wp_sitemaps_posts_query_args
wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php 237-251
$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
);

Where the hook is used in WordPress

Usage not found.
3 comments