feat: fallback to post type slug for parent page#59
Conversation
Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
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
$postTypeand$supportsinto variables for reuse/readability. - Falls back to
get_post_type_object($postType)->rewrite['slug']whenparent-pagesupport does not provide a slug.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $supports = get_all_post_type_supports($postType); | ||
| $parentPageSlug = $supports['parent-page'][0]['slug'] ?? get_post_type_object($postType)->rewrite['slug'] ?? null; |
There was a problem hiding this comment.
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).
| $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; |
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