WordPress custom post_type archive url and default query

I’m now using wordpress 4.2, and developing themes on it for my own site.

I defined many custom post types in my theme, and I’m now finding a way to make some archive pages for them.

Read More

For example, I have a post_type called shop, and have a taxonomy named region assigned to it.

I to created a template file: category-region.php

Then I visit the permalink of this template: http://example.com/category/region/, this template was matched, and init the query with all assigned shops, then display them.

But now I want to make a page with all posts of shop, no matter which region they are assigned.

How should I name the template file and what about the permalink?


I checked for the template hierarchy and have found the below diagram.

https://developer.wordpress.org/themes/basics/template-hierarchy/

enter image description here

It seems archive-$post_type.php is what I wanted.

But what about the permalink that is pointing to this template?

Related posts

1 comment

  1. The rewrite parameter in the arguments you pass to register_post_type determines the permalink for your custom post type archive.

    https://codex.wordpress.org/Function_Reference/register_post_type

    Looking at the example in the documentation:

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );
    
    register_post_type( 'book', $args );
    

    You can see that the rewrite slug for the book CPT is book, so the URL to the book archive would be example.com/book/.

    In your case, if you set your rewrite slug to shop (or if you didn’t set a rewrite slug, in which case it would default to your CPT slug), the URL that would correspond to your archive-shop.php template would be example.com/shop/.

    Note that this assumes you are using permalinks, if you are not using permalinks the shop post type archive is located at example.com/?post_type=shop. If you are using permalink, make sure you refresh them after setting up your CPT by visiting the Settings > Permalinks page.

Comments are closed.