# One Template for All Custom Post Types

> Lets every custom post type use a shared archive-custom and single-custom template before falling back to the general ones, so you design CPT pages once.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: templates, themes, custom-post-types
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/shared-custom-type-templates/

## Code

```php
add_filter( 'archive_template_hierarchy', function ( $templates ) {
    if ( ! is_post_type_archive() || is_post_type_archive( 'post' ) ) {
        return $templates;
    }

    $pos = array_search( 'archive.php', $templates, true );
    array_splice( $templates, false === $pos ? count( $templates ) : $pos, 0, array( 'archive-custom.php' ) );

    return $templates;
} );

add_filter( 'single_template_hierarchy', function ( $templates ) {
    $type = get_post_type( get_queried_object_id() );

    if ( ! $type || in_array( $type, array( 'post', 'attachment' ), true ) || get_post_type_object( $type )->_builtin ) {
        return $templates;
    }

    $pos = array_search( 'single.php', $templates, true );
    array_splice( $templates, false === $pos ? count( $templates ) : $pos, 0, array( 'single-custom.php' ) );

    return $templates;
} );
```

## Notes

Create archive-custom.php and single-custom.php in a classic theme, or templates named archive-custom and single-custom in a block theme. Post-type-specific templates still win.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "One Template for All Custom Post Types" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
