0

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.
  • technically the archive slug should be /article/ not /articles/. The default archive.php file should be able to display your articles. – Shazzad Apr 12 at 20:16
  • /article also does not work – morpheus Apr 12 at 21:12
  • Where is the code in your question located? Do permalinks work for other post types and pages? What are your permalinks set to? – Tom J Nowell Apr 13 at 16:06
  • Permalinks are set to /?p=123 in /wp-admin/options-permalink.php – morpheus Apr 13 at 16:20
1

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.

| improve this answer | |
  • I have tried both those things as well. using /article and resaving permalinks. I also tried looking at the WP error log wp-content/debug.log and there is nothing in it. I can access my article if I goto /?article=test-article – morpheus Apr 13 at 16:02
  • So no permalinks work for your article post type at all? That's new and very important information you did not share. – Tom J Nowell Apr 13 at 16:06
0

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

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.