• Resolved pyoil

    (@pyoil)


    Hi

    I am trying to remove the slug in my custom post type following the instructions here: https://michaelbox.net/2020/06/slugless-post-type-permalinks/

    I have the following two codes I got from the website:

    function cptui_demo_remove_slug_from_permalink_string( $post_link, $post ) {
    
    	if ( 'publish' !== $post->post_status ) {
    		return $post_link;
    	}
    
    	if ( 'hub' === $post->post_type ) {
    		$post_link = str_replace(
    			'/' . $post->post_type . '/',
    			'/',
    			$post_link
    		);
    	}
    
    	return $post_link;
    }
    add_filter( 'post_type_link', 'cptui_demo_remove_slug_from_permalink_string', 10, 2 );
    
    function cptui_demo_add_post_type_to_query( $query ) {
    
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;
    	}
    
    	if ( empty( $query->query['name'] ) ) {
    		return;
    	}
    
    	$query->set(
    		'post_type',
    		[
    			'post',
    			'page',
    			'hub',
    		]
    	);
    }
    add_action( 'pre_get_posts', 'cptui_demo_add_post_type_to_query' );
    

    After implementing the code I saved the permalink structure in settings. But I am still getting the 404 error.

    I believe this is because this post is a child to another post and the same is reflected in the URL. Meaning

    domain.com/hosting is the parent and has no 404 error here. But domain.com/hosting/wp-engine-review is the child and it gets a 404 error.

    What can I do so that the child page doesn’t get any 404 error.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Which is the post type slug? “hub” or “hosting”? If hub, do you have the rewrite setting set up to change “hub” to “hosting” with this post type?

    If you’re setting the rewrite to change hub to hosting, then I think you need to specify “hosting” inside the cptui_demo_remove_slug_from_permalink_string() function.

    However, if the original permalink was /hub/hosting/wp-engine-review/ then I’m not quite sure yet what may be going on or why.

    Thread Starter pyoil

    (@pyoil)

    The post type slug is hub

    I used the code in your blog to remove the word hub from the URL and it works when the CPT has no parent.

    But when the CPT has a parent it gives 404.

    So domain.com/hosting works but domain.com/hosting/wp-engine-review gives 404 error. This second link is the child of the first link.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I was able to recreate but this is also a new one to me, so I don’t have an immediate solution.

    At least from what I’m seeing, the child slug seems to be getting interpreted as an attachment instead of a page.

    I’m curious which is more important in your situation, the removal of the post type slug or the parent/child hierarchy.

    Thread Starter pyoil

    (@pyoil)

    Both are important. I want the post hierarchy to be maintained and the slug to be removed.

    I found this solution that is actually working but it seems overly complex and I am not sure if it is breaking more things than fixing.

    I had to remove your code and replace it with the one on this page: https://wordpress.stackexchange.com/questions/114723/removing-base-slug-from-hierarchical-custom-post-type

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Whatever gets the job done is what should be used, and if this is working, awesome.

    Based on what I’m seeing, it looks like it’s all pretty well guarded and only affecting what it should be, but I haven’t done any testing to verify myself.

    Sometimes customizing WordPress like this takes complex looking solutions.

    Thread Starter pyoil

    (@pyoil)

    Thanks. Please update this thread if you have an updated or refined solution.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Removing Slug Gives 404 in Child Posts’ is closed to new replies.