Redirect page for a custom post type

I have the following custom post type

        register_post_type('cp_clients', array(
            'labels' => array(
                'name' => __('Clients'),
                'singular_name' => __('Client')
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array('thumbnail', 'title', 'editor')
                )
        );

I want to output custom content when the user calls http://blogurl.com/companies. It’s a plug-in and not a theme.

Read More

Any ideas how I can redirect to a custom page/output for this URL?

Related posts

Leave a Reply

3 comments

  1. I don’t think you need a redirect. You are missing the rewrite rule for your custom post type.

    register_post_type('cp_clients', array(
        'labels' => array(
            'name' => __('Clients'),
            'singular_name' => __('Client')
        ),
        'public' => true,
        'has_archive' => true,
        'supports' => array('thumbnail', 'title', 'editor')
        'rewrite' => array('slug'=>'companies')
    );
    

    You should now be able to create and use single-cp_clients.php, and archive-cp_clients.php template files. And your urls will look like example.com/comapnies and example.com/comapnies/acmecorp

  2. Thank you for your answers. I found a solution to the problem. The solution is to use “template_redirect” and to detect if the page is the post type archive page

     if ($wp_query->query_vars['post_type'] === 'cp_companies' && $wp_query->query_vars['name'] === '') {
    
    }