HIBIIRO
How to Consolidate WordPress Custom Post Types into Standard Posts Without Breaking URLs
On Hibiiro, site announcements (news) and movie reviews (movie) were originally managed as separate WordPress Custom Post Types.
While having distinct URL prefixes for each type was neat, splitting them across multiple post types cluttered the admin sidebar and fragmented categories and workflow.
To simplify content management, I consolidated all entries into standard WordPress post items and used a lightweight post meta key (_hibiiro_kind) to preserve dedicated URL structures where needed.
Here is how you can merge custom post types into standard posts without breaking existing permalinks or archive URLs.
- Decoupling Database Storage Type from Permalink Structure
- 1. Generating Custom Permalinks via post_link
- 2. Translating Custom URLs into Query Variables
- 3. Validating Both Post ID and Kind in the request Filter
- 4. Filtering Archive Pages via pre_get_posts
- Testing Both Valid and Invalid Permalink Combinations
Decoupling Database Storage Type from Permalink Structure
After the migration, regular blog articles, announcements, and movie reviews are all stored in WordPress with post_type = 'post', distinguished by the _hibiiro_kind meta key:
| Entry Kind | post_type | _hibiiro_kind | Example URL |
|---|---|---|---|
| Standard Article | post | post | /post/123/ |
| Announcement | post | news | /news/456/ |
| Movie Review | post | movie | /movie/789/ |
To make this work seamlessly, two pieces must work together:
- Outbound link generation: Return the custom URL pattern whenever WordPress generates a link for that post.
- Inbound request routing: Resolve incoming requests for
/news/456/to the matching standard post and verify its kind.
1. Generating Custom Permalinks via post_link
add_filter('post_link', [__CLASS__, 'link'], 30, 3);
public static function link($url, $post, $leavename = false) {
$kind = self::kind($post->ID);
if ($post->post_type !== 'post' || $kind === 'post') {
return $url;
}
return home_url(
user_trailingslashit($kind . '/' . $post->ID)
);
}If post 456 has _hibiiro_kind = 'news', get_permalink(456) returns /news/456/. Standard articles return the default $url untouched.
2. Translating Custom URLs into Query Variables
add_action('init', function () {
add_rewrite_rule(
'^news/([0-9]+)/?$',
'index.php?hibiiro_kind=news&hibiiro_id=$matches[1]',
'top'
);
});
add_filter('query_vars', function ($vars) {
$vars[] = 'hibiiro_kind';
$vars[] = 'hibiiro_id';
return $vars;
});For example, the rewrite rule turns /news/456/ into these query variables:
hibiiro_kind = news
hibiiro_id = 4563. Validating Both Post ID and Kind in the request Filter
If we only resolved the numeric ID, someone could access a movie review #789 via /news/789/. To prevent duplicate URLs, the request filter checks that the URL kind matches the post’s stored kind:
public static function request($vars) {
if (!empty($vars['hibiiro_id'])) {
$id = absint($vars['hibiiro_id']);
$post = get_post($id);
$kind = $vars['hibiiro_kind'] ?? '';
if (
!$post ||
!in_array($kind, self::special_kinds(), true) ||
self::kind($id) !== $kind ||
$post->post_type !== 'post'
) {
return ['error' => '404'];
}
$vars['p'] = $id;
$vars['post_type'] = $post->post_type;
}
return $vars;
}4. Filtering Archive Pages via pre_get_posts
For archive routes like /news/, a rewrite rule sets hibiiro_archive=news, and pre_get_posts appends a meta_query clause on the main frontend query:
$clause = [
'key' => '_hibiiro_kind',
'value' => $kind,
];
$old = $q->get('meta_query');
$q->set(
'meta_query',
$old
? ['relation' => 'AND', $old, $clause]
: [$clause]
);Testing Both Valid and Invalid Permalink Combinations
| Test URL | Expected Behavior |
|---|---|
Valid /news/ID/ | Renders announcement post |
Mismatched /movie/NEWS_ID/ | Returns HTTP 404 |
| Non-existent ID | Returns HTTP 404 |
/news/ archive & page 2 | Lists only announcements with working pagination |
| Private / draft post | Blocked for logged-out visitors by WordPress core |
By separating how posts are stored in the database from how URLs are routed, the admin dashboard stays clean while every existing link continues to work without a hitch.