我想在我的YOAST网站地图中排除所有比过去2年更老的帖子。我使用wpseo_exclude_from_sitemap_by_post_ids过滤器来做这件事。
function exclude_posts_from_xml_sitemaps()
{
global $wpdb;
/*$date = new DateTime('first day of this year');
$date_str = $date->format('Y-m-d H:i:s');*/
$date_str = '2018-01-31 00:00:00';
$query = " SELECT ID FROM $wpdb->posts WHERE post_date < '$date_str' AND post_type = 'post' ORDER BY post_date DESC";
$results = $wpdb->get_results($query);
foreach ($results as $result) {
$array[] = $result->ID;
}
return $array;
}
add_filter('wpseo_exclude_from_sitemap_by_post_ids', 'exclude_posts_from_xml_sitemaps');
一切正常,但是sitemap_index.xml仍然显示一些(空的) post-sitemap1.xml、post-sitemap2.xm等等。
似乎主站点地图在添加之前没有检查相关子站点地图的内容是否大于0。
有什么建议吗?
发布于 2020-11-20 01:27:24
已解决:
add_filter( 'wpseo_posts_where', 'mysitemap_where_filter' );
add_filter( 'wpseo_typecount_where', 'mysitemap_where_filter' );
function mysitemap_where_filter( $where ) {
$date = new WP_Date_Query(
array(
'column' => 'post_date_gmt',
'after' => '1 year ago', // Custom logic
)
);
return $where . ' ' . $date->get_sql();
}
请注意:重新生成站点地图后添加代码(Yoast设置开启/关闭站点地图)。
希望能帮助别人。
https://stackoverflow.com/questions/64916539
复制相似问题