Manage a Custom Post Type Archive’s content, meta, title, and location through a WordPress Page instead of squirreling away content elsewhere.
This plugin makes it possible to set Static Pages for Custom Post Types, much like how you can set a Static Front Page or a Posts Page through the WordPress Reading Settings.
The plugin extends this WordPress core feature as close as possible.
- The Page URL overrides the custom post type’s
has_archive
setting. - The Page Title takes the place of the post type archive label setting.
- An “Edit Archive Page” link is added to the WordPress Admin Bar on the front-end.
- “— (Post Type Label) Page” is added to the Pages Edit Screen next to the Page title, like how WordPress adds “— Posts Page” or “— Home Page” next to the Blog and Home pages.
- Any additional content or post meta set on the Page is available for display on the post type archive.
- There’s also a feature to specify a 404 page so that the 404 page content can be managed through the WordPress admin.
The static page for post types feature is not activated for all custom post types by default. To enable it, add the following argument to register_post_type
:
has_post_type_page => true
If you don’t have access to the post type registration code, filter the post type registration args like this:
add_filter( 'register_post_type_args', function ( $args, $post_type ) {
$args['page_for_post_type'] = true;
return $args;
}, 1, 2 );
Use 'post_type_page_disable_editor' => false,
to keep the editor enabled for the custom post type archive page. This way, you can include content on a post type archive page that's intended to supplement the posts loop. You will most likely need to customize your theme in order to wire up this content to the archive page.
To set a Page for a Custom Post Type, go to the WordPress Reading Settings. Under “Pages for Custom Post Types,” you’ll see all post types that are available, and a select box of pages. Select a page and save.
Once you’ve set a Page for a Post Type, you can get that page in your themes and plugins by using the get_option
function. If you had a post type named “book,” the function call would be:
$books_page = get_option( 'page_for_book' );
Since WordPress uses the option name page_for_posts
and not page_for_post
, we also try to store the plural version of the post type name by tacking a "s" to the end of the post type name if the post type does not already end in "s."
For example, if the post type is named "book," either of these options will work:
$books_page = get_option( 'page_for_book' );
$books_page = get_option( 'page_for_books' );
On the other hand, if the post type is named "books," the only option that will work is page_for_books
.