Search code examples
wordpresshttp-status-code-404permalinksslug

Wordpress remove permalink from custom posts but retain archive


I have a Wordpress site where I keep track of publications with an archive of custom post types called "publications". Each publication post shouldn't have its own page, it just needs to appear in the archive page. Right now when I create a publication called "test publication" there is a page created at www.mysite.com/publications/test-publication. I've seen the suggestion of changing the post option public to false, but then I can't access the archive itself, it just redirects to the home page. If I add publicly_queryable => true in addition to public => false then I can get to the archive page, but the dedicated page for each publication shows up again. I need it to give me a 404 if I try to visit www.mysite.com/publications/test-publication but still allow me to access the archive. Help, am I missing something obvious?


Solution

  • You can have template redirect added so that singular link if accessed redirects to Archive page:

    add_action( 'template_redirect', 'disable_singular_publications' );
    
    function disable_singular_publications()
    {
        if ( ! is_singular( 'publications' ) )
            return;
    
        wp_redirect( get_post_type_archive_link( 'publications' ), 301 );
        exit;
    }
    

    You may add above function in functions.php , code is not tested so you may need to check on any typos or syntax errors.