0

I have custom post type "project", and it is loading with the template "archive-project".

I already have a page "applications" to load "archive-project"

So the same page is accessible via both URL's

www.example.com/project  
www.example.com/applications  

I tried a plugin "smart Archive Page Remove", which doesn't work for me.

How to remove the www.example.com/project

Please help.

  • 1
    It's loading with the template archive-project (i.e. archive-project.php)? Why can't you just delete that file? – tmdesigned Apr 28 at 8:55
  • @tmdesigned When i delete archice-project, design breaks – Vivek Apr 28 at 9:03
  • Do you use a custom query in archive-project.php and given it a Template Name that you assigned to applications page? – Qaisar Feroz Apr 28 at 9:35
  • @QaisarFeroz both are loading same archive-project.php – Vivek Apr 28 at 9:40
  • @Vivek, that is clear from your question that both urls load same template file. I wanted to know how archive-project.php is codded. Please reply to my previous comment clearly so I may be able to resolve the issue or at least guide you in right direction. – Qaisar Feroz Apr 28 at 9:51
1

It seems that your archive-project.php is using a custom query to display the posts. And you have declared it as Page template by adding Template Name in a comment block, so you are able to assign it to applications page. As the result www.website.com/applications loads this template file.

www.website.com/project also uses the same template file because of its file name archive-project.php. This is how Wordpress works.

Wordpress uses its Template File Hierarchy to display posts and pages on front-end.

For Custom Post Types with 'has_archive' => true wordpress use the following order of template files to render the archive:

  1. archive-{post_type}.php – If the post type is project, WordPress will look for archive-project.php
  2. archive.php
  3. index.php

The post displayed by above files are selected and fetched from db using main query, so no custom query is required here.

However, some developers choose to create a Custom Page Template for displaying the archive of posts. This file can have any valid file name e.g. my-awsome-template.php. However, care should be taken to avoid file names that collide with standard wordpress template file names. e.g. archive.php or archive-project.php etc.

These template files are created by adding Template Name in a comment block, something like this.

<?php
/*
Template Name: My Custom Page
*/

It’s a good idea to choose a name that describes what the template does as the name is visible to WordPress users when they are editing the page. For example, you could name your template Homepage, Blog, or Portfolio.

Now this template can be assigned to any pages on the Page > Edit screen.
One of the characteristic of custom templates is the use of custom queries to fetch and display posts from db usin WP_Query or get_posts() and generally does not use Wordpress main query.

The Solution

How to remove the www.website.com/project

You are using custom page template to display the archive and do not want to use mechanism used by wordpress to display archive of posts. So set 'has_archive' => false where you register your post type project. It is also recommended to rename the archive-project.php to something else.

  • 1
    "However, some developers choose to create a Custom Page Template for displaying the archive of posts." Which is incorrect, and the reason a question is posted every other day asking why pagination is broken. Archives should use the template hierarchy and default query. – Jacob Peattie Apr 28 at 12:34
  • @quisar I tried 'has_archive' => false. but now www.website.com/project is loading the template index.php – Vivek Apr 28 at 13:16
  • @Vivek you need to flush your rewrite rules. In admin area visit Settings -> Permalinks and click "Save Changes" to flush your rewrite rules. – Qaisar Feroz Apr 28 at 13:23

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.