12

I've created a post type called listing and an accompanying archive page archive-listing.php. When I navigate to /listings, the archive page loads properly. However, I'd like the archive URL to be /marketplace. I'm defining the rewrite property below but it doesn't seem to be working. What am I missing?

register_post_type("listing",
    array(
        "menu_icon" => "dashicons-tag",
        "labels" => array(
            "name" => __( "Listings" ),
            "singular_name" => __("Listing")
        ),
        "public" => true,
        "has_archive" => true,
        "rewrite" => array("slug" => "marketplace"),
        "supports" => array("title", "editor"),
        "taxonomies" => array("listing_status")
    )
);
5
  • 2
    You're going about it the right way. You have flushed the permalink rules, right (one way would be going to Settings -> Permalinks in the admin and hitting save)? That needs to be done after you've set the new slug. Commented Sep 9, 2016 at 0:43
  • In addition to Nathan Dawson's advice, make sure there is no page or post with the same slug, as it may take precedence over the archive in the rewrite rules. What is the error you are getting?
    – Pim Schaaf
    Commented Sep 9, 2016 at 4:02
  • Thanks! Flushing the permalink rules did it. I didn't realize I had to do that. Commented Sep 9, 2016 at 15:50
  • @NathanDawson if you post your comment as an answer I'll go ahead and select your answer, since you were the first one to respond. Thanks! Commented Sep 9, 2016 at 15:53
  • @DavidJones - thanks, will do Commented Sep 9, 2016 at 16:15

3 Answers 3

8

You can also use:

'has_archive' => 'marketplace'

That way the slug of the archive page will be /marketplace and the single page will be /listing

1
  • 1
    this should be the accepted answer .. simple and effective solution. thanks !
    – moped
    Commented Jul 14, 2020 at 13:43
6

Changing the archive slug independently of single posts

The default slug for a custom post type will match the post type key (first argument of the function). In the example you posted, that would be listing. You then go on to change it by specifying a new slug under rewrite. That will impact both the single post and the archive however.

To set the archive slug while preserving the regular slug for single posts, you should use has_archive instead. has_archive is a misleading name since it accepts either a boolean or a string. Setting it to true will generate the archive page. Passing in a string instead will allow you to customize the slug:

'has_archive' => 'marketplace',

Example in context:

register_post_type( 'listing', [
    'menu_icon' => 'dashicons-tag',
    'labels' => [
        'name' => __( 'Listings' ),
        'singular_name' => __( 'Listing' ),
    ],
    'public' => true,
    'has_archive' => 'marketplace',
    'supports' => [ 'title', 'editor' ],
    'taxonomies' => [ 'listing_status' ],        
]);

Flushing permalinks

When you register a new custom post type you need to flush your permalinks.

To do that, go to Settings -> Permalinks in the admin and hit save (no changes are required).

If you're creating something that's going to be distributed you may want to do it programmatically instead. You'd need to run flush_rewrite_rules() by hooking into the plugin activation or after_switch_theme action. Documentation can be found here: https://codex.wordpress.org/Function_Reference/flush_rewrite_rules

0

Try updating Settings->Permalink structure to default, and then to %%postname%%

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.