Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellenmace/fae42a47342d0ee4fe4a to your computer and use it in GitHub Desktop.
Save kellenmace/fae42a47342d0ee4fe4a to your computer and use it in GitHub Desktop.
Remove custom post type slug from permalinks 2
<?php
/**
* Have WordPress match postname to any of our public post types (post, page, race).
* All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts.
* By default, WordPress only accounts for posts and pages where the slug is /post-name/.
*
* @param $query The current query.
*/
function gp_add_cpt_post_names_to_main_query( $query ) {
// Bail if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Bail if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Bail if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'race' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );
@komapeb
Copy link

komapeb commented May 3, 2018

Is there a way to make this work with hierarchical CPT?
E.g.
example.com/parent-1/subpage
example.com/parent-2/subpage

Subpage has the same name, with a different parent, and has different contents. Just the slugs are identical.

@Garconis
Copy link

Garconis commented Jul 3, 2019

@kellenmace is this still working? For some reason, this still allows me to create a slug on my CPT that already exists on a live Page.

@kellenmace
Copy link
Author

Hey @Garconis - Yeah, this still works and has for several years. I would make sure you’re using both code snippets from my blog post, though: https://kellenmace.com/remove-custom-post-type-slug-from-permalinks/ If you are missing the other one, that may be why WP is allowing your custom post type and pages to have the same slugs.

@TailonR
Copy link

TailonR commented May 22, 2020

Your blog post said to create a plugin, do I insert both snippets into one php file? or two?

@kellenmace
Copy link
Author

@TailonR Both snippets can go in the same PHP file. So the plugin will end up looking something like this: https://gist.github.com/kellenmace/f8a3393385f01ee226a087ff19cc6056

@TailonR
Copy link

TailonR commented May 26, 2020

@kellenmace Thank you once I did that, I change "race" on lines 16 and 49, and for consistency, on line 25, and it worked.

@kellenmace
Copy link
Author

@TailonR Nice! Glad to hear it. 👍🏼

@stevek28
Copy link

@kellenmace could you please help me with the version of your script to remove the slug for multiple cpt's at once? I tried your answer to this on kallenmace.com from June23, 2015 (attached) but I simply can't get it to work..

Remove-Custom-Post-Type-Slug-from-Permalinks-Kellen-Mace

I try to remove the slug from 'example1' and 'example 2' with the following code -> only 'example2' is removed,
and I get a syntax error /unexpected $EOF as well..

Thank you so much!

Greetings, Stefan

``<?php

/**

  • Remove the slugs from published post permalinks. Only affect our custom post types, though.
    */
    function gp_remove_cpt_slug( $post_link, $post ) {

    $custom_post_types=array( 'example1', 'example2' );

    if ( !in_array( $post->post_type, $custom_post_types ) ||'publish' !=$post->post_status ) {

    return $post_link;

    }

add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 2 );

/**

  • Have WordPress match postname to any of our public post types (post, page, example1, example2).

  • All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts.

  • By default, WordPress only accounts for posts and pages where the slug is /post-name/.

  • @param $query The current query.
    */
    function gp_add_cpt_post_names_to_main_query( $query ) {

    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
    return;
    }

    // Bail if this query doesn't match our very specific rewrite rule.
    if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
    return;
    }

    // Bail if we're not querying based on the post name.
    if ( empty( $query->query['name'] ) ) {
    return;
    }

    // Add CPT to the list of post types WP will include when it queries based on the post name.
    $query->set( 'post_type', array( 'post', 'page', 'example1', 'example2' ) );
    }
    add_action ( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );

@kellenmace
Copy link
Author

@stevek28 It looks like you're missing the $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); line and the curly brace after that. I assume that didn't get copied & pasted in properly. Other than that, your code looks correct to me.

Have you tried flushing the permalinks by to Settings > Permalinks in the WordPress admin and re-saving your settings (even if you haven’t changed anything on that page)?

What happens if you list example2 first and then example1 after in both places? Does only the example1 slug get removed if you do that?

@stevek28
Copy link

stevek28 commented May 1, 2021

@kellenmace, Hi Kellen thx for your answer! Oops I lost that line somewhere.. Now the error is gone but the slugs (both) are not removed..
Permalinks saved a few times, caches cleared, clean install, latest WP, CPT's created with CPT UI.. For a single CPT your script still woks fine..
Any Ideas? Here is the code I have right now:

`/**

  • Remove the slug from published post permalinks. Only affect our custom post type, though.
    */
    function gp_remove_cpt_slug( $post_link, $post ) {

    $custom_post_types=array ( 'example1', 'example2' );

    if ( ! in_array( $post->post_type, $custom_post_types ) ||'publish'!=$post->post_status ) {
    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }

    return $post_link;
    }
    add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 2 );

/**

  • Have WordPress match postname to any of our public post types (post, page, race).

  • All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts.

  • By default, WordPress only accounts for posts and pages where the slug is /post-name/.

  • @param $query The current query.
    */
    function gp_add_cpt_post_names_to_main_query( $query ) {

    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
    return;
    }

    // Bail if this query doesn't match our very specific rewrite rule.
    if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
    return;
    }

    // Bail if we're not querying based on the post name.
    if ( empty( $query->query['name'] ) ) {
    return;
    }

    // Add CPTs to the list of post types WP will include when it queries based on the post names.
    $query->set( 'post_type', array( 'post', 'page', 'example1', 'example2' ) );
    }
    add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );`

Thank you so much for your help! Stefan

@abhivr
Copy link

abhivr commented Jul 20, 2021

The code works for Parent Pages but for the child pages.
I am trying to remove the word 'slug' here

domain.com/slug/hosting (works)
domain.com/slug/hosting/wp-engine (getting 404 error)

My CPT is hierachical where the second links is the child of the first link. I am getting 404 error for the second.

@wwdes
Copy link

wwdes commented Mar 25, 2023

code does not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment