admin管理员组

文章数量:1122846

I have a custom post type containing job listings, that is highly volatile. Jobs are frequently added and removed. Our analytics show that a lot of crawling errors are for detail pages of jobs that have been unlisted.

The solution I came up with is to redirect all visits for non-existant URLs within the CPT's slug to the job listing overview, and I would like to automate this.

How would I go about this? I'm looking for a solution that does this very early and skips as much unnecessary calls as possible. (i.e. something earlier than doing this in header.php, ideally as an action)

Example:

  • mydomain/jobs/existingjob/ delivers the detail page for the job
  • mydomain/jobs/nojobhere/ does not exist and would throw a 404 but instead gets redirected to mydomain/jobs/

I have a custom post type containing job listings, that is highly volatile. Jobs are frequently added and removed. Our analytics show that a lot of crawling errors are for detail pages of jobs that have been unlisted.

The solution I came up with is to redirect all visits for non-existant URLs within the CPT's slug to the job listing overview, and I would like to automate this.

How would I go about this? I'm looking for a solution that does this very early and skips as much unnecessary calls as possible. (i.e. something earlier than doing this in header.php, ideally as an action)

Example:

  • mydomain.com/jobs/existingjob/ delivers the detail page for the job
  • mydomain.com/jobs/nojobhere/ does not exist and would throw a 404 but instead gets redirected to mydomain.com/jobs/
Share Improve this question asked May 31, 2017 at 10:33 Alina_0xFFAlina_0xFF 231 gold badge1 silver badge4 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 4

It looks like template_redirect is as far up the WordPress action chain you can go while still being able to detect that a 404 error is being thrown. This action will trigger before any template files would be loaded so it will happen before loading unnecessary resources.

You can try adding this into your /wp-content/themes/yourtheme/functions.php file to achieve a dynamic redirect for all 404's that happen when viewing single jobs:

add_action( 'template_redirect', 'unlisted_jobs_redirect' );
function unlisted_jobs_redirect()
{
    // check if is a 404 error, and it's on your jobs custom post type
    if( is_404() && is_singular('your-job-custom-post-type') )
    {
        // then redirect to yourdomain.com/jobs/
        wp_redirect( home_url( '/jobs/' ) );
        exit();
    }
}

I don't have the rep to comment on Ben HartLenn's answer, and I know this is an old post, but to save future users from wasting time: is_singular('post_type') will not work on a 404 page. $wp_query->queried_object will also not work.

This does work:

add_action( 'template_redirect', 'unlisted_jobs_redirect' );
function unlisted_jobs_redirect(){
    //check for 404
    if( is_404()){
        global $wp_query;
        //check that wp has figured out post_type from the request
        //and it's the type you're looking for
        if( isset($wp_query->query['post_type']) && $wp_query->query['post_type'] == 'your-job-custom-post-type' ){
        // then redirect to yourdomain.com/jobs/
        wp_redirect( home_url( '/jobs/' ) );
        exit();
    }
}

If there is no page on your site with that post type, you need to ask yourself the question: should I really be deleting that page? Or should I just make it better? If you decide to make sure you send the proper HTTP header: a 410 content deleted header.

404 and 410 HTTP headers The difference between a 404 and a 410 header is simple: 404 means “content not found”, 410 means “content deleted” and is thus more specific. If a URL returns a 410, Google is far more certain you removed the URL on purpose and it should thus remove that URL from its index. This means it will do so much quicker.

You can use Yoast SEO Premium plugin, the redirects module in this plugin is capable of serving 410 headers.

本文标签: phpHow do I redirect all 404 errors of a specific post type to another URL