WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say that I want to make a post type called 'press' and it is mostly concerned with linking a title with a PDF document of a press clipping. I want to show all of these as an archive... so something like site.com/press but i don't want any single post template pages. so no site.com/press/article1 or site.com/press/article2. other than not including a link in my archive template (which just obscures it but doesn't negate their existence or prevent access to the single posts) how can i prevent a visitor from inadvertently accessing the single posts. how could i re-direct them back to the /press archive?

share|improve this question
up vote 12 down vote accepted

The fast way

In your .htaccess add a rule

RedirectMatch Permanent ^/press/.+ /press/

Plugin way

Hook into template_redirect and redirect all requests to a single entry:

add_action( 'template_redirect', 'wpse_45164_redirect_press' );

function wpse_45164_redirect_press()
{
    if ( ! is_singular( 'press' ) )
        return;

    wp_redirect( get_post_type_archive_link( 'press' ), 301 );
    exit;
}

(Caveat: not tested)

share|improve this answer
    
@toscho is there an argument which does this when registering a custom post type? Or is redirecting the only way to prevent access to a single custom post type page? – Anagio Feb 2 '13 at 16:31
    
@Anagio The alternative is: make the post type not public, register an endpoint to show the archive. But the you have to fiddle with the query (pagination, plugins don’t hook in when they should etc.). The solution I gave in my answer is the most pragmatic approach I could come up with. – toscho Feb 2 '13 at 16:52

An alternative to redirecting users would be to make it so this page isn't generated to begin with. Setting your post type to 'public' => false, 'publicly_queryable' => true will create a non-public post type. Then you can build a custom page template to act as the archive, with a custom query in the page template.

See the register_post_type function for more info.

share|improve this answer
    
I don't believe that your answer is correct. When publicly_queryable is set to true, both the Archive and Singles will properly render. – trailing slash Jan 11 at 20:00
    
By default publicis set to false. This answer is not correct. From the Codex‌​: 'true' - Implies exclude_from_search: false, publicly_queryable: true, show_in_nav_menus: true, and show_ui:true. The built-in types attachment, page, and post are similar to this. 'false' - Implies exclude_from_search: true, publicly_queryable: false, show_in_nav_menus: false, and show_ui: false. – Piet Apr 12 at 4:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.