wp_sitemaps_posts_query_args
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'] : array();
// Exclude posts
$args['post__not_in'][] = 12;
$args['post__not_in'][] = 24;
return $args;
}Changelog
| Since 5.5.0 | Introduced. |
| Since 6.1.0 | Added ignore_sticky_posts default parameter. |
Where the hook is called
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 );