I am developing a website in WordPress and I have used a custom post type for people to use. The custom post type only shows if the user is logged in and is the author of it, this shows on the profile page and nowhere else.

What I want to do is stop people being able to search for it in the address bar, for example..

http://www.website.co.uk/custom-post-type/post-1 - This will show the single version of this post, however I need it to redirect to the login page.

http://www.website.co.uk/custom-post-type/ - This shows an archive page of the custom post type, however I need this to redirect to the login page. I have solved this temporaroly with a 301 redirect, however if you have any other advice, I would appreciate it.

Any help would be great!

1 Answer 1


You could use template_redirect hook to redirect your pages, like this:

function my_page_template_redirect() {
    global $wp_query;
    $object = $wp_query->get_queried_object();
    //this returns queried object. Check the conditions and redirect
add_action('template_redirect', 'my_page_template_redirect');
  • Thanks for the speedy reply. I have this code which I beleive should work, however it doesn't... function my_page_template_redirect(){ if( $post->post_type == 'custom-post-type' ) { wp_redirect( home_url( '/login/' ) ); exit(); } } add_action( 'template_redirect', 'my_page_template_redirect' ); The way I think this should work is, if the post is of a custom-post-type then redirect to the login page.
    – sims
    Commented Dec 1, 2014 at 9:32
  • try $object->post_type
    – lcshobin
    Commented Dec 1, 2014 at 9:45
  • dump $object check how to get post_type name. $object returns object.
    – lcshobin
    Commented Dec 1, 2014 at 9:47
  • Good news, I have currently just managed to get the archive page of the custom post type to redirect which is half the battle using the following code... (Please excuse the formatting of the code, I haven't got the hang of posting properly) function toolkits_redirect() { $posttype = 'toolkits'; if (is_post_type_archive($posttype)) { wp_redirect( home_url( '/agency-login/' ) ); exit(); } } add_action('template_redirect', 'toolkits_redirect');
    – sims
    Commented Dec 1, 2014 at 10:02
  • My second solution for redirecting the single template of the post, so people cant search for... website.co.uk/custom-post-type/post-1 I used the single_template in order to give my custom post type a different single template then I placed a header redirect in that template to redirect to the desired page. Thank you for the help!! If you need the code I shall pop it on.
    – sims
    Commented Dec 1, 2014 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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