When your website means business.

CPT, WP/CP | Type: PHPCreate sub posts for a WordPress custom post type

This has to do with creating hierarchical CPTs, so you can have ‘sub-posts’ as children of a CPT single.

// Fake pages' permalinks and titles. Change these to your required sub pages.
$my_fake_pages = array(
    'reviews' => 'Reviews',
    'purchase' => 'Purchase',
    'author' => 'Author Bio'
);

add_filter('rewrite_rules_array', 'fsp_insertrules');
add_filter('query_vars', 'fsp_insertqv');

// Adding fake pages' rewrite rules
function wpse_261271_insertrules($rules)
{
    global $my_fake_pages;

    $newrules = array();
    foreach ($my_fake_pages as $slug => $title)
        $newrules['books/([^/]+)/' . $slug . '/?$'] = 'index.php?books=$matches[1]&fpage=' . $slug;

    return $newrules + $rules;
}

// Tell WordPress to accept our custom query variable
function wpse_261271_insertqv($vars)
{
    array_push($vars, 'fpage');
    return $vars;
}

// Remove WordPress's default canonical handling function

remove_filter('wp_head', 'rel_canonical');
add_filter('wp_head', 'fsp_rel_canonical');
function wpse_261271_rel_canonical()
{
    global $current_fp, $wp_the_query;

    if (!is_singular())
        return;

    if (!$id = $wp_the_query->get_queried_object_id())
        return;

    $link = trailingslashit(get_permalink($id));

    // Make sure fake pages' permalinks are canonical
    if (!empty($current_fp))
        $link .= user_trailingslashit($current_fp);

    echo '<link rel="canonical" href="'.$link.'" />';
}

Reference Links

Menu
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Cookies Notice