Hot answers tagged

161

Use the global variable $post: <?php global $post; $post_slug = $post->post_name; ?>


73

As per other answers, slug is stored in the post_name property. While it could be accessed directly, I prefer the (underused) get_post_field() function for accessing post properties which have no proper API for them. It requires post provided explicitly and doesn't default to the current one, so in full for the current post it would be: $slug = ...


68

The following code will work, but you just have to keep in mind that conflicts can happen easily if the slug for your custom post type is the same as a page or post's slug... First, we will remove the slug from the permalink: function na_remove_slug( $post_link, $post, $leavename ) { if ( 'events' != $post->post_type || 'publish' != $post->...


50

Use get_page_by_path($page_path): $page = get_page_by_path( 'about' ); echo get_the_title( $page ); This will return a regular post object.


30

Use get_posts() and the parameter name which is the slug: $page = get_posts( array( 'name' => 'your-slug' ) ); if ( $page ) { echo $page[0]->post_content; } Be aware that the post type in get_posts() defaults to 'post'. If you want a page use … $page = get_posts( array( 'name' => 'your-slug', 'post_type' => 'page' ...


27

EDIT 5 APRIL 2016 After digging for more reliability, I ended up doing this answer to the following post which leads to this edit: (Be sure to check it out) $GLOBALS['wp_the_query'] vs global $wp_query The most reliable method till date I could come up with is the following: // Get the queried object and sanitize it $current_page = sanitize_post( ...


25

Yes, this is possible, but if the plugin is creating a custom post type using the rewrite => array('slug' => 'post_type') parameter, then it's not likely that you're going to be able to replace the slug. Whenever custom post types are created, URL rewrite rules are written to the database. Depending on which action triggers the creation of the custom ...


22

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 altered your custom post type taxonomy document, try to go to Settings > Permalinks and re-save your settings, else you will get 404 page not found.


18

Figured out: function re_rewrite_rules() { global $wp_rewrite; // $wp_rewrite->author_base = $author_slug; // print_r($wp_rewrite); $wp_rewrite->author_base = 'autor'; $wp_rewrite->search_base = 'buscar'; $wp_rewrite->comments_base = 'comentarios'; $wp_rewrite->pagination_base = 'pagina'; $...


18

Off the bat, I can't give you a page/tutorial/documentation on how WP slugs are generated, but take a look at the sanitize_title() function. Don't get a wrong impression by the function name, it is not meant to sanitize a title for further usage as a page/post title. It takes a title string and returns it to be used in a URL: strips HTML & PHP strips ...


17

Yes, it is possible. Sample code, has to be tested and refined: // get all posts $posts = get_posts( array ( 'numberposts' => -1 ) ); foreach ( $posts as $post ) { // check the slug and run an update if necessary $new_slug = sanitize_title( $post->post_title ); if ( $post->post_name != $new_slug ) { wp_update_post( ...


17

For some sites in German I use the following plugin to translate page to seite (the German word for page): <?php # -*- coding: utf-8 -*- /** * Plugin Name: T5 Page to Seite * Description: Ersetzt <code>/page/</code> durch <code>/seite/</code>. * Author: Fuxia Scholz * License: MIT * License URI: http://www.opensource....


16

The easiest workaround could be: function myplugin_update_slug( $data, $postarr ) { if ( ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { $data['post_name'] = sanitize_title( $data['post_title'] ); } return $data; } add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );


16

I've been using this .. function get_id_by_slug($page_slug) { $page = get_page_by_path($page_slug); if ($page) { return $page->ID; } else { return null; } } Hope this will help someone.


14

The simple way to get the slug is with: <?php echo basename(get_permalink()); ?>


13

The following 2 filters allow you to hook into when WordPress checks the slug and are found in the function wp_unique_post_slug() in the wp-includes/post.php file. There are 2 filters, one for hierarchical posts and one for non-hierarchical. The hierarchical filter provides the ID for the post parent so if the $post_parent is 0, you know this is a "base" ...


13

No, forward slashes are not allowed to be used in slugs. However, they are automatically stripped out when trying to include slashes in a slug on the post editing screen. Slugs are sanitized by using sanitize_title(). Unfortunately, there is no easy way to explain what is and isn't allowed in a slug, let alone to give a simple set of rules to which valid ...


13

I tried to figure this out not long ago and the short answer from what I know is no. Not from within the rewrite argument at least. The long explanation becomes apparent if you look at the actual code of register_post_type in wp-includes/post.php line 1454: add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args ); You ...


11

I just went and tested this (WP 3.3.1), the wordpress core will automatically do a redirect if you change the post slug, even if you change the slug from post-a to post-b and then to post-c, post-a will return a 301 to post-c. That said, the redirection plugin that Mike Jolley recommended is an outstanding plugin and a great way to monitor for 404 errors and ...


11

Due to wp_posts table structure the length of post_name column (the column for slugs) is equal to 200 characters.


11

Create a Page Template Add a new page and give it the slug stores Add A Public Query Variable add_filter('query_vars', 'add_state_var', 0, 1); function add_state_var($vars){ $vars[] = 'state'; return $vars; } Add a rewrite rule This will direct your request to your stores page. add_rewrite_rule('^stores/([^/]*)/?','index.php?post_type=page&...


11

The parameter to insert a custom slug is: 'post_name' => 'my-custom-slug' Not post_slug as one would think! :)


11

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;


10

use pagename= to query by slug. See WP_Query for full list of valid arguments.


10

This is an old post but can't you just use wordpress $menu and/or $submenu globals like Oleg suggested in number 2. When in doubt copy WordPress: wordpress/wp-admin/menu.php For example to add link this seems like it would work: function add_external_link_admin_submenu() { global $submenu; $permalink = admin_url( 'edit-tags.php' ).'?taxonomy=...


10

It has been already asked and answered on this forum. I am pasting the same code from there. Use this function to retrieve page id. function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) { global $wpdb; $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND ...


9

It's in the table postmeta with the meta_key of _wp_old_slug If you wanted to clear all of the old slugs you could use this: DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';


9

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 you change 'custom_post_type' for your own CPT name throughout my example below. There are many occurrences, and a 'find/replace' is an easy way to catch them ...


8

Given the code example, it looks like what you really need is a link. In that case, you can use get_permalink(), which can be used outside of the loop. That should do what you need more reliably than using the post slug.


8

Permalinks like http://example/שָׁלוֹם are actually working in my WordPress 3.3. Could be the remove_accents() improvements for i18n permalinks. As Sean & Steve noted, make sure you're using WordPress ≥ 3.3 make sure your .htaccess file contains a rule similar to RewriteRule . /index.php [L] check that your database is UTF-8 encoded (and consider ...


Only top voted, non community-wiki answers of a minimum length are eligible