1

I have a custom post type acme_reviews. I've namespaced it as suggested in tutorials to prevent future conflicts. But I want its archive page to simply be acme.com/reviews, not acme.com/acme_reviews. How do I achieve this? This is my code:

function create_reviews_post_type() {
    register_post_type('acme_reviews', array(
        'labels' => array(
            'name' => __('Reviews'),
            'singular_name' => __('Review')
            ),
        'menu_position' => 5,
        'public' => true,
        'has_archive' => true,
        )
    );
}
add_action('init', 'create_reviews_post_type');

2 Answers 2

3

The register_post_type has_archive option also accepts a string. That string will be used for the archives. See the changes in the code below:

function create_reviews_post_type() {
    register_post_type('acme_reviews', array(
        'labels' => array(
            'name' => __('Reviews'),
            'singular_name' => __('Review')
            ),
        'menu_position' => 5,
        'public' => true,
        'has_archive' => 'reviews',
    );
}
add_action('init', 'create_reviews_post_type');
2
  • Perfect - thank you. In the unlikely case anyone else doesn't know this, I'll just add that I had to update my permalinks after making this change.
    – Kleeramne
    Oct 26, 2016 at 11:58
  • Note: If you want different (shorter) name for admin and frontend archive title, just define with is_admin() like 'name' => is_admin() ? __('Reviews') : __('Reviews frontend archive title'), ... do NOT use this type of code for post type key!
    – jave.web
    Jan 8, 2018 at 21:34
1

According to the codex (and what works for me!) is to just use a rewrite in the array. Both options work but wordpress suggests using the rewrite Below is the quote directly from the codex:

has_archive (boolean or string) (optional) Enables post type archives. Will use $post_type as archive slug by default.

Default: false

Note: Will generate the proper rewrite rules if rewrite is enabled. Also use rewrite to change the slug used.

MAKE SURE TO FLUSH THE PERMALINK RULES. if this is in a plugin i always suggest adding an init that flushes the rules when the plugin is activated.

Here is the code, notice i customized the function name too for the same reasons that you customize the post name itself.

function create_acme_reviews_post_type() {  //namespaced your function too..
    register_post_type('acme_reviews', array(
        'labels' => array(
            'name' => __('Reviews'),
            'singular_name' => __('Review')
            ),
        'menu_position' => 5,
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'reviews' ), //changes permalink structure
        )
    );
}
add_action('init', 'create_acme_reviews_post_type'); //namespaced function call too..
3
  • I don't agree that the Codex is saying that this is the preferred way. I just think they are letting you know that if you have set rewrite to false this won't work. Since rewrite is enabled by default you don't need to do this. This will definitely still work and is totally valid. I just wanted to make that point.
    – Fencer04
    Oct 25, 2016 at 17:23
  • 1
    yes both ways could work i'm not arguing that either. My example and op had has_archive set to true. my point is that the codex doesn't even mention the method you described, it states that has_archive can take a string but doesn't show why or use it in any of the examples, whereas using the rewrite option is both commented upon and used in the examples.
    – rudtek
    Oct 25, 2016 at 17:43
  • Well put, makes sense.
    – Fencer04
    Oct 25, 2016 at 17:45

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.