NO BUG: MetaBox CPT hierarchical children Invalid Post Type (Custom Rewrite Slug)

Browser: Chrome 111.0.5563.65
OS: Windows

Very similar to a bug that was previously reported - (NO BUG: MetaBox CPT hierarchical children Invalid Post Type)
When trying to edit child posts of a custom post type, I am getting an error to say invalid post type. You can see from the screenshot below that the child pages don’t have the bricks tag next to it.

What’s stranger is that if I change the status to draft, I am able to edit with bricks, but not when published.

I have the following code running to remove the “/services” cpt slug from the permalink.


Any help would be much appreciated!

Hi Owais,
Thanks so much for your report!

The error is clearly in your code because without it there is no problem.
Can you possibly provide the code as code, and not as a screenshot?

At the top of the toolbar there is a small </> icon :slight_smile:

Best regards,
timmse

Sure, thanks!

Please see below:

<?php

function na_remove_slug( $post_link, $post, $leavename ) {

    if ( 'services' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );

<?php

function na_parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'services', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request' );
1 Like

I have the same problem… also using a plugin that removes the CPT slug.
As soon as I try to open the Bricks Templates I get the error “Invalid Post Type”

Hi @Adstract ,

Thanks for the code. However, the error is not related to Bricks.

Once you applied those code, the rewrite rules in WordPress is messed up when you have a child post.

( Example: yoursite.com/service-1/sub-service, WordPress don’t know what page is this)
You can try to view the child post in frontend and you should be directed to the 404 page.

You will see the same in different themes as well.

PS: Query monitor plugin Request tab can help you understand currently using which rewrite rules on the page.

Regards,
Jenn

1 Like