首页 > 解决方案 > 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。

有什么建议吗?

标签: wordpressyoast

解决方案


解决了:

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 设置打开/关闭站点地图)。

希望能帮助某人。


推荐阅读