32
votes
Remove slug from custom post type post URLs
Write following code into the taxonomy registration.
'rewrite' => [
'slug' => '/',
'with_front' => false
]
Most important thing that you have to do after code changing
After you’ve ...
32
votes
Remove slug from custom post type post URLs
Looking through the answers here I think there is room for a better solution that combines some things I learned above and adds auto-detection and prevention of duplicate post slugs.
NOTE: Make sure ...
29
votes
Accepted
How to set post slug when using wp_insert_post();?
The parameter to insert a custom slug is:
'post_name' => 'my-custom-slug'
Not post_slug as one would think! :)
29
votes
Accepted
How to get slug of current category in taxonomy template?
You can get the term object of the category you’re viewing with get_queried_object(). That will contain the slug.
$term = get_queried_object();
echo $term->slug;
19
votes
Accepted
How to get custom post type label and singular label from its slug?
get_post_type_object() will return, as the name suggests, an object that contains the post type information.
You may want to var_dump() it to inspect it contents. You'll see that it includes (among ...
16
votes
Remove slug from custom post type post URLs
Plugin Roundup
It's almost 2020 and a lot of these answers don't work. Here's my own roundup of the current options:
Matt Keys answer seems to be the only one on the right track if you want a custom ...
16
votes
Accepted
How to rewrite slug of custom post type archive page
Pass the desired archive slug via has_archive:
$args = array(
'rewrite' => array( 'slug' => 'case-studies' ),
'has_archive' => 'testimonials-case-studies',
);
15
votes
Redeclare/Change Slug of a Plugin's Custom Post Type
This code worked well for my child theme. Needed to change "program" slug to "place".
/*
CHANGE SLUGS OF CUSTOM POST TYPES
*/
function change_post_types_slug( $args, $post_type ) {
/*item post ...
13
votes
Is sanitize_title enough to generate post slugs?
sanitize_title() seems to be the only one you need.
In wp-includes/default-filters.php line 211 you will find:
add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3);
This means that ...
12
votes
How can I find plugins' slug?
The difference between the plugin (main) file and the plugin's slug is a place where the WordPress Codex could do much better.
I understand your confusion as I have felt it too recently (mixed with ...
10
votes
Accepted
Custom post type with slug for plural (archive) and for single
Both of these are controlled by the arguments passed to register_post_type, specifically, the rewrite and has_archive arguments:
$args = [
'rewrite' => ['slug' => 'article'],
'...
9
votes
Accepted
Exclude specific slug in 'get_terms'
The get_terms() (see docs) function accepts the same args as WP_Term_Query(see docs)
You have to get those terms Ids first and then pass it to the exclude arg:
// default to not exclude terms
$...
7
votes
Can I have two child pages of different parents with the same name?
Yes, you can.
In the old days, this was not possible (as evident from other answers). However, it's possible now.
WordPress now lets child pages (of any post type that is hierarchical) from ...
7
votes
Remove slug from custom post type post URLs
Background
Even after looking around everywhere, I couldn't find a proper solution for removing CPT slug from permalinks that actually works and is consistent with how WordPress actually parses ...
7
votes
Accepted
Custom Taxonomy leads to 404 page
You can add flush_rewrite_rules() after registering your custom taxonomy.
NOTE:
Flush rules only on activation or deactivation, or when you know that
the rewrite rules need to be changed. Don't ...
7
votes
Accepted
Custom Post type & Taxonomy URL structure
After forever, I figured out an answer!
First: we register the custom post type & custom taxonomy:
add_action( 'init', 'register_sps_products_post_type' );
function register_sps_products_post_type(...
7
votes
Accepted
Don't replace "|" with Empty String ("") when generating slugs from title
When WordPress inserts a post, it runs the title through a filter called sanitize_title to get the slug. By default there is a function called santize_title_with_dashes attached to this filter with ...
7
votes
Yoast SEO breadcrumbs: how to create a filter that uses the url slug for breadcrumb titles
Yoast does have a filter for you to use. See here:
https://gist.github.com/jmcclellan/b2d97ffee0e924e28fa1
I used this to add "parent" pages to custom post types. We wanted to use pages as our ...
7
votes
Accepted
Post slug changed using code doesn't reflect on editor when post is published
I guess you are using Gutenberg in which case this is non-trivial problem.
Gutenberg saves post data in two steps (two separate http requests, just use developer tools in your browser to check). ...
5
votes
how to get page id of a page using page slug
There is a function url_to_postid since WordPress v1.0.0 :) This task is easiest to achieve by using this function.
url_to_postid
When page is top-level page, only slug has to be given.
e.g. ...
5
votes
How to check if a slug exists?
How about this simpler approach?
$post_exists = get_page_by_path( $slug, OBJECT, $post_type );
if ( ! $post_exists )
echo 'No post exists with this slug.';
If a post doesn't exist for the given ...
5
votes
Is sanitize_title enough to generate post slugs?
Well, there is already an answer, but I wanted to expand it a bit, so here are my findings:
If we have a look in wp_insert_post() we see, the $post_name is sanitized using wp_sanitize_title() ( see ...
5
votes
How do I retrieve the slug of the current page?
You can simply explode the slug from the request.
global $wp;
// Since slugs itself can't contain slashes,
// let's explode on slashes and get just the last portion.
$request_args = explode('/', $wp-&...
5
votes
Rewrite rules for custom post type slug
WordPress seems to be unable to generate the correct rules when you use more complex formats for the slug argument in register_post_type. In this case we have to use add_permastruct.
This example ...
5
votes
Accepted
How can I get the page url slug when 'post_name' returns an id?
Try this :
$menu = get_term( $locations[$theme_location], 'nav_menu' );
$menu_items = wp_get_nav_menu_items($menu->term_id);
foreach( $menu_items as $menu_item ) {
$link = $menu_item->url;...
5
votes
Accepted
Hook on slug generator
I think you should be able to do this using the wp_unique_post_slug filter (which is applied in the function of the same name):
add_filter("wp_unique_post_slug", function($slug, $post_ID, $...
5
votes
How do I use the same post slug for different Categories?
Using Parent-Child Page (Recommended)
If you don't have to have categories & posts, then this can be easily achieved using parent-child pages (not posts).
For example, say you have three pages ...
4
votes
How do I retrieve the slug of the current page?
Might be an old question, but I created the functions get_the_slug() and the_slug() based on your answers.
if ( !function_exists("get_the_slug") ) {
/**
* Returns the page or post slug.
*
...
4
votes
Is it possible to get a page link from its slug?
Try This:
<a href="<?php echo get_page_link( get_page_by_path( 'map' ) ); ?>">Map</a>
get_page_by_path( 'path' ) returns page/post object which can be then used by get_page_link() ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
slug × 762permalinks × 187
custom-post-types × 181
url-rewriting × 90
urls × 82
custom-taxonomy × 81
categories × 79
posts × 50
taxonomy × 45
pages × 43
php × 38
redirect × 37
rewrite-rules × 35
terms × 34
customization × 31
functions × 23
plugins × 21
author × 20
title × 16
woocommerce-offtopic × 15
archives × 15
404-error × 15
filters × 14
id × 14
wp-query × 13