New custom PHP pages are getting 404s

When I create new php pages and try to visit them I am receiving a 404. For example, I have created the file page-tacos.php (stored in my child theme directory) but when I try to visit the URL at localhost:8888/tacos I am getting a 404.

I have added several custom pages in the past and whenever I was having this issue I would follow the advice from this question and it would fix my problem. However, now flushing my rewrite rules is not fixing it.

I have tried renaming my file to not include the 'page-' prefix, resaving my permalinks, and running flush_rewrite_rules( false ); to no avail.

My .htaccess file has not changed.

Does anyone have any other suggestions?

Answers 2

  • Things have been done backwards here, in a rather literal way. Templates don't determine the content/URL. Content/URL determines the template.

    If you create a page test at /test it will load page.php, but you can add page-test.php and it will choose the most specific template it has available, allowing you to override just that page.

    Here is what happens when you load a URL:

    • WP processes the URL
    • The URL gets turned into query parameters ( the same ones that go into WP_Query or get_posts ), this is where permalinks/rewrite rules are done
    • It looks at these parameters and figures out if it's an archive or a search page etc
    • It then runs the query and fetches the posts
    • THEN it figures out which template to load, based on the parameters.

    It's a common misconception that WP takes a URL and loads a template, and which template is loaded determines the content, but that's not true.

    WP uses the template hierarchy to figure out which template to load, so you can use that to make its template selection more specific/bespoke.

Related Questions