Question

I am using Wordpress 5.4. I created a CPT using the code below:

$args = array(
    'labels'        => $labels,
    'description'   => 'Some description',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
register_post_type( 'article', $args ); 

has_archive was set to true and my understanding is that now when I goto /articles I am supposed to see all posts of type article. But that is not the case. When I goto /articles I see my normal posts. I do not have any article-archive.php on my system and understand that is not necessary. I do have a ./wp-content/themes/twentynineteen/archive.php and I am using a child theme of twnetynineteen on my site.

How can I fix this?

EDIT:

  1. I have tried /article and it does not fix the problem.
  2. I have tried resaving permalinks (Settings->Permalinks) and it does not fix the problem.
  3. The code above is located in a plugin.
  4. I can see my article if I goto /?article=test-article where test-article is slug of my article.
  5. I am using nginx with this configuration.
Was it helpful?

Solution 2

The solution to this problem is to use Post name in /wp-admin/options-permalink.php.

enter image description here

OTHER TIPS

has_archive was set to true and my understanding is that now when I goto /articles I am supposed to see all posts of type article.

Nowhere in your code is articleS mentioned, the slug of the post type is article, so the URL of its archive should be /article/. If you want it to be articles you have to say so.

In this case, it's the classic mistake of registering a post type or taxonomy, but not flushing permalinks to add their rules. Go to the permalinks setting page, and resave.

As for the template archive.php, this is irrelevant to wether the page loads or not. The template hierarchy will fallback to index.php which is required for a theme if no other templats are found. Additionally, archive.php doesn't make the URL an archive. The URL makes it an archive, and the fact it's an archive makes WP load that template. WP has already figured out what posts to fetch before the template is chosen.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top