首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >YOAST:从sitemap中排除旧帖子: works但sitemap索引显示sitemap空白

YOAST:从sitemap中排除旧帖子: works但sitemap索引显示sitemap空白
EN

Stack Overflow用户
提问于 2020-11-20 01:07:18
回答 1查看 42关注 0票数 0

我想在我的YOAST网站地图中排除所有比过去2年更老的帖子。我使用wpseo_exclude_from_sitemap_by_post_ids过滤器来做这件事。

代码语言:javascript
运行
复制
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。

有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2020-11-20 01:27:24

已解决:

代码语言:javascript
运行
复制
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设置开启/关闭站点地图)。

希望能帮助别人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64916539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档