n5s/page-for-custom-post-type

Page for custom post types, just like page for posts

dev-main 2023-10-14 09:25 UTC

This package is auto-updated.

Last update: 2024-04-14 10:30:43 UTC


README

WordPress custom post type archive are dynamically generated pages that can't really be edited.

If you dealed with it, you probably faced some recurring issues, among those:

  • You might want to give your client the ability to add some content to that page (cover, title, excerpt, content, custom fields, etc.) just like any other page.
  • You might want to customize SEO settings

There has been lots of efforts to circumvent this issues:

Although these plugins provided great inspiration to design this plugin, none of them were really satisfying.

Approach

This plugin tries to solve this problem by taking advantage of the native WordPress behavior, just like it does for the posts page. Which means (almost) no extra query or new function to get your page object.

In a posts page request (show_on_front=page, page_for_posts={id}), the $wp_query will contain both objects:

  • $wp_query->queried_object: the custom post type archive page (WP_Post)
  • $wp_query->posts: the custom post type posts (WP_Post[])

The whole idea is to mimic this behavior for custom post types, hence the name.

Once activated, your custom post type will appear in Settings > Reading admin page.

Capture d’écran 2023-06-16 à 12 01 40

Choose any page you want to set your page for custom post type.

Key differences with CPT archive

CPT archive Home for CPT
Conditionals is_post_type_archive = true
is_archive = true
is_home = true
is_$posttype_page = true
is_page_for_custom_type = $posttype
Queried object WP_Post_Type WP_Post
Template hierarchy archive-$posttype.php
archive.php
index.php
home-$posttype.php
home.php
index.php

API

To be documented, in the meantime, check the src/functions.php file for getting an overview of available functions.

Integrations

This plugin provides integrations with:

  • ACF: add a new condition rule
  • WordPress SEO
  • Polylang