Skip to content

feat: fallback to post type slug for parent page#59

Merged
ictbeheer merged 1 commit into
mainfrom
feat/parent-page-slug
May 1, 2026
Merged

feat: fallback to post type slug for parent page#59
ictbeheer merged 1 commit into
mainfrom
feat/parent-page-slug

Conversation

@ictbeheer
Copy link
Copy Markdown
Member

@ictbeheer ictbeheer commented Apr 28, 2026

For the overwhelming majority of post types the parent page slug is equal to the post type slug.
This PR still allows a different slug, but introduces a fallback to the post type slug if no parent-page slug is configured.

See also Brave Components

Co-authored-by: Copilot <copilot@github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a fallback mechanism for resolving a “parent page” by using the current post type’s rewrite slug when an explicit parent-page support slug is not configured.

Changes:

  • Extracts $postType and $supports into variables for reuse/readability.
  • Falls back to get_post_type_object($postType)->rewrite['slug'] when parent-page support does not provide a slug.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Traits/ParentPage.php
Comment on lines +35 to +36
$supports = get_all_post_type_supports($postType);
$parentPageSlug = $supports['parent-page'][0]['slug'] ?? get_post_type_object($postType)->rewrite['slug'] ?? null;
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_post_type_object($postType)->rewrite['slug'] can throw warnings/fatal errors if get_post_type($postId) returns false (invalid post) or if get_post_type_object() returns null, and rewrite can be false (non-rewritten CPT) so array access may warn. Consider guarding for a falsy $postType, storing the post type object in a variable, and only reading $obj->rewrite['slug'] when the object exists and rewrite is an array with a slug key (otherwise fall back to null).

Suggested change
$supports = get_all_post_type_supports($postType);
$parentPageSlug = $supports['parent-page'][0]['slug'] ?? get_post_type_object($postType)->rewrite['slug'] ?? null;
if (! $postType) {
return [];
}
$supports = get_all_post_type_supports($postType);
$postTypeObject = get_post_type_object($postType);
$rewriteSlug = is_object($postTypeObject) && is_array($postTypeObject->rewrite) && isset($postTypeObject->rewrite['slug'])
? $postTypeObject->rewrite['slug']
: null;
$parentPageSlug = $supports['parent-page'][0]['slug'] ?? $rewriteSlug;

Copilot uses AI. Check for mistakes.
@ictbeheer ictbeheer merged commit 0d2a107 into main May 1, 2026
6 checks passed
@ictbeheer ictbeheer deleted the feat/parent-page-slug branch May 1, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants