Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom post type with an archive slug that differs from the post type name

Tags:

php

wordpress

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")
    )
);
like image 253
David Jones Avatar asked Sep 09 '16 00:09

David Jones


People also ask

How do I create a custom post type slug in WordPress?

php $postType = get_post_type(); $related = new WP_Query([ 'post_type' => $postType, 'posts_per_page' => 3, 'post__not_in' => [ $post->ID ], ]);

How do I create a custom archive page for custom post type in WordPress?

Let's start by adding the Posts block to display items from your custom post type. Simply drag and drop the Posts block in the Advanced section onto your page. By default, the posts block will display your blog posts. Click on the block settings and then select your post type under Query by Post Type section.

What is a post type archive?

Custom Post Type Archives The term “Archive” refers to a list of entries like post types, categories, and tags. To display archives on your website, they must be enabled for a custom post type. You will usually find the URL of your custom post type archive looking like this: http://example.com/post-type-slug/


1 Answers

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

like image 168
Biels Avatar answered Nov 08 '22 08:11

Biels