Custom post-type in root directory, blog posts in subdirectory

moving blog posts to /blog/ is pretty easy and you can use WordPress settings to do that – no coding needed.

enter image description here

The problem you will have though is that after moving posts, your CPT URL structure will conflict with URL structure of pages – so you’ll have to modify your pre_get_posts function.

As for your post_type_link you can simplify it a lot (if it is not hierarchical… is it?):

add_filter( 'post_type_link', function($post_link, $post, $leavename) {
    if ('custom' == $post->post_type && 'publish' == $post->post_status) {
        $post_link = site_url("https://wordpress.stackexchange.com/") . $post->post_name ."https://wordpress.stackexchange.com/";
    }

    return $post_link;

}, 10, 3 );

And I also wouldn’t use pre_get_posts for selecting proper posts. Using parse_request is a lot easier for this task and it allows you to prevent 404 errors a lot easier.

add_action( 'parse_request', function ( $wp ) {
    if ( ! is_admin() ) {
        if ( array_key_exists('error', $wp->query_vars) && '404' == $wp->query_vars['error'] ) {
            $custom_post = get_page_by_path( $wp->request, OBJECT, 'CUSTOM' );

            if ( $custom_post instanceof WP_Post ) {
                unset( $wp->query_vars['error'] );
                $wp->query_vars['page'] = '';
                $wp->query_vars['CUSTOM'] = $wp->query_vars['name'] = $wp->request;
                $wp->query_vars['post_type'] = 'CUSTOM';
            }            
        }
    }

    return $wp;
} );

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)