admin管理员组

文章数量:1205744

I'd like to return a 410 code for posts (of custom type "news") which have previously been published (and therefore have a url indexed by search engines), but have now had the post status set to draft.

Here's my attempt:

function news_gone() {
    global $post;
    if ( is_singular( 'news' ) && get_post_status($post->id) == "draft"  ) {
        global $wp_query;
        $wp_query->set_404();
        status_header(410);
    }
}
add_action( 'template_redirect', 'news_gone' );

This, however, has no effect - is template_redirect the correct hook, or is there a better approach?

本文标签: redirectHow to return a 410 code for previously published posts now set to draft