WordPress Yoast如何从XML站点地图中排除文章

3

我正在使用Yoast SEO插件,并尝试使用“wpseo_sitemap_entry”过滤器手动从帖子XML站点地图中排除帖子,但迄今为止没有成功。

这是我的当前代码:

function sitemap_exclude_post( $url, $type, $post) {
    if($post->ID == 6298 ) {
        return false;
    } 

return $url;
}
add_filter( 'wpseo_sitemap_entry', 'sitemap_exclude_post', 1, 3 );

非常感谢您的建议。

注意:我知道可以通过Yoast插件的后端手动输入文章ID来完成,但我需要通过过滤器来完成。上述代码将在以后进一步更改,以便从WordPress中的特定类别自动获取文章(single.php)的文章ID。


你的解决方案应该是可行的,因为它对我有效,而 false 是正确的方式来告诉 Yoast 不要包含链接。你确定 ID 正确吗?你可以尝试在 add_filter 中更改优先级,但我不认为这会改变任何东西。 - Picard
1
感谢您的查看。是的,ID是正确的。代码现在已经可以工作了,我只是在Yoast的仪表板“排除文章”部分中按下了“保存更改”按钮,然后它就可以工作了。不确定是怎么回事,但它正在完成工作。 - Waqas
4个回答

4
我知道这个问题已经有答案了,但我需要更具动态性的方法来解释@clodclod91所说的内容,因为我收到了不止一次的提问,所以我编写了一些代码,以"更流畅的方式"来实现这个功能,而不是硬编码任何值。
我希望它能帮助一些人。
我还写了一篇带有更多解释的文章。在这里阅读。
/**
 * Set all excluded items for sitemap in a transient
 *
 * @return mixed
 */
function beee_get_sitemap_excludes_transient() {

    // get transient
    $output = get_transient( 'beee_exclude_sitemap' );

    // if transient returns false, create it
    if ( false == $output ) {

        $exclude_args = array(
            'post_type'      => [ 'page' ], // change this to the post types you want excluded
            'posts_per_page' => -1, // all items of course
            'meta_query'     => array(
                array(
                    'key'     => 'beee_exclude_sitemap',
                    'value'   => '1',
                    'compare' => '=',
                ),
            ),
            'orderby'        => 'ID',  // recommend to set to ID so 'the order is easier to compare later on', otherwise it can create issues
            'order'          => 'ASC', // same, just sort ASC to avoid issues
        );
        $excluded_items = get_posts( $exclude_args );
        if ( count( $excluded_items ) > 0 ) {
            // if there are items and create an empty array
            $exclude = [];
            foreach( $excluded_items as $item ) {
                // add post id to array
                $exclude[] = $item->ID;
            }
            // create a string from an array since Yoast stores a string, not an array
            $output = implode( ',', $exclude );

            // set transient with a 24-hour expiration
            set_transient( 'beee_exclude_sitemap', $output, 24 * HOUR_IN_SECONDS );
        }
    }

    return $output;
}

/**
 * Set Yoast settings from transient
 */
function beee_exclude_pages_sitemap( $post_id ) {

    // get our excluded items from transient
    $our_excluded_posts = beee_get_sitemap_excludes_transient();

    // check if 'exclude' field has been set (in the post)
    if ( 1 == get_field( 'beee_exclude_sitemap', $post_id ) ) {
        // if yes, check if it exists in transient
        if ( in_array( $post_id, explode( ',', $our_excluded_posts ) ) ) {
            // yes, it's already included so we don't have to do anything
        } else {
            // no, it's not included so it needs to be added
            // so we delete the transient (existing values)
            delete_transient( 'beee_exclude_sitemap' );
        }
    } else {
        // it has not been set in the post
        if ( in_array( $post_id, explode( ',', $our_excluded_posts ) ) ) {
            // if does exists in our stored transient, which it shouldn't be in there
            // so we delete the transient (existing values)
            delete_transient( 'beee_exclude_sitemap' );
        }
    }

    // get our excluded ids from transient
    // since we just cleared it, the transient doesn't exist and is retrieved from scratch
    $our_excluded_posts = beee_get_sitemap_excludes_transient();

    // get post ids from Yoast settings
    $wpseo_xml = get_option( 'wpseo_xml' );

    // compare Yoast's value with our items
    if ( $wpseo_xml[ 'excluded-posts' ] == $our_excluded_posts ) {
        // they are the same so we don't need to do anything
    } else {
        // they are not the same so we need to update the database with our new ids
        update_option( 'wpseo_xml', $wpseo_xml );
    }
}
add_action( 'save_post', 'beee_exclude_pages_sitemap' );

3

可以使用update_option函数。

$array = array(
  "excluded-posts" => 6298
);

update_option('wpseo_xml', $array);

应该也可以在add_filter钩子中工作。update_option是wp函数,用于在options表中插入或更新数据。Yoast SEO的XML网站地图在wp_options表中的关键字是wpseo_xml。值中有一个包含其他信息的数组:

a:18: {
s: 22: "disable_author_sitemap";
b: 1;
s: 22: "disable_author_noposts";
b: 1;
s: 16: "enablexmlsitemap";
b: 1;
s: 16: "entries-per-page";
i: 1000;
s: 14: "excluded-posts";
s: 7: "1,2,3,4";
s: 38: "user_role-administrator-not_in_sitemap";
b: 0;
s: 31: "user_role-editor-not_in_sitemap";
b: 0;
s: 31: "user_role-author-not_in_sitemap";
b: 0;
s: 36: "user_role-contributor-not_in_sitemap";
b: 0;
s: 35: "user_role-subscriber-not_in_sitemap";
b: 0;
s: 36: "user_role-authors_tes-not_in_sitemap";
b: 0;
s: 40: "user_role-authors_academy-not_in_sitemap";
b: 0;
s: 30: "post_types-post-not_in_sitemap";
b: 0;
s: 30: "post_types-page-not_in_sitemap";
b: 0;
s: 36: "post_types-attachment-not_in_sitemap";
b: 1;
s: 34: "taxonomies-category-not_in_sitemap";
b: 0;
s: 34: "taxonomies-post_tag-not_in_sitemap";
b: 0;
s: 37: "taxonomies-post_format-not_in_sitemap";
b: 0;

}


2

我在问题中发布的上述代码可以正常工作。

我只是在Yoast的仪表板“排除帖子”部分内按下“保存更改”按钮,它就起作用了。不确定为什么,但它正在工作。


1

谢谢您的回复。我知道这一点,但在我的情况下,这需要通过代码完成。 - Waqas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接