wordpress - YOAST:从站点地图中排除旧帖子:有效,但站点地图索引显示站点地图为空白
问题描述
我想在我的 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。
有什么建议吗?
解决方案
解决了:
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 设置打开/关闭站点地图)。
希望能帮助某人。
推荐阅读
- reactjs - Next.js 链接在 /api 路由上获得 404 状态码
- java - 为什么不能两次调用 scanEverything 方法?它只打印出arrayList一次,第二个println显示一个空列表
- sql - 添加 IF ELSE 语句
- c++ - 我们可以定义两个同名但参数不同的函数吗?
- html - Vue.js - 根据选择更改数学运算符?
- javascript - global.HermesInternal - 属性 'HermesInternal' 在类型 'Global & typeof globalThis' 上不存在
- django - 模型中带有 json 字段的 Django crud api
- javascript - 无法读取未定义的属性“添加事件侦听器”
- python-3.x - flask_wtf 中无法识别自定义验证器
- c - hrtimer_init() 中的 hrtimer 定时器模式与 hrtimer_start() 中的到期模式