Hot answers tagged

89

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 = ...


81

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->...


61

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. Documentation: https://developer.wordpress.org/reference/functions/get_page_by_path/ https://developer.wordpress.org/reference/functions/get_the_title/


34

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( ...


28

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.


19

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.


19

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


18

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 ...


17

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


17

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;


16

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 ...


14

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 ...


12

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=...


11

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 code solution. None of the plugins I found can do everything listed here, especially the duplicate checking. This approach seems like a really good opportunity ...


11

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 other stuff) another object, labels that contains all the registered labels for the specific post type. $pt = get_post_type_object( 'books' ); // These two ...


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 ...


10

Pass the desired archive slug via has_archive: $args = array( 'rewrite' => array( 'slug' => 'case-studies' ), 'has_archive' => 'testimonials-case-studies', );


9

This is what you're looking for, tested and I use it on my own sites: function the_slug_exists($post_name) { global $wpdb; if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) { return true; } else { return false; } } You can then use it like this: if (the_slug_exists('...


9

There is no need to reregister the default post post type. You can just add a prefix to the permalink structure using the Permalinks settings, found under the main Settings menu in your WordPress administration panel:


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';


8

This can also be caused by uploading images with the same file name as the post/page. In my case, I uploaded an image called 'careers.jpg' before I created a page called 'Careers', so the page was getting the '-2' appended. To fix it, I just changed the slug on the image, then I was able to change the slug on the page.


8

An attachment is just a post with the post_status = inherit and the post_type = attachment, so can be queried with WP_Query or get_posts. Note that slug (post_name) is unique per post type. $_header = get_posts('post_type=attachment&name=book-site-header&posts_per_page=1&post_status=inherit'); $header = $_header ? array_pop($_header) : null; $...


8

The string used in WordPress to identify the plugin is: plugin_basename($file); … where $file is a file with the plugin headers. So if you are in your plugin, get the slug with: $slug = plugin_basename( __FILE__ );


8

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 frustration). This is what I have learned by doing some "detective work" on the WordPress core code. The plugin file This is the unique way WordPress ...


8

This rewrite rule should work (assuming "brand" and "type" are the taxonomy registered names): function custom_rewrite_rules() { add_rewrite_rule('^brand/(.*)/type/(.*)?', 'index.php?brand=$matches[1]&type=$matches[2]', 'top'); } add_action('init', 'custom_rewrite_rules'); Remember to flush the rewirte rules after saving this code in your site. ...


8

In response to my previous answer: you could of course set the rewrite parameter to false when registering a new post type and handle the rewrite rules yourself like so <?php function wpsx203951_custom_init() { $post_type = 'event'; $args = (object) array( 'public' => true, 'label' => 'Events', 'rewrite' ...


8

Here is what I did to implement this: function slug_save_post_callback( $post_ID, $post, $update ) { // allow 'publish', 'draft', 'future' if ($post->post_type != 'post' || $post->post_status == 'auto-draft') return; // only change slug when the post is created (both dates are equal) if ($post->post_date_gmt != $post->...


8

Use: get_category_link( $category_id ); See: https://codex.wordpress.org/Function_Reference/get_category_link In your specific case: <?php global $post; $categories = get_the_category(); foreach ($categories as $category) : $exclude = get_the_ID(); $posts = get_posts('posts_per_page=4&category='. $category->term_id); ...


7

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 type slug*/ if ( 'program' === $post_type ) { $args['rewrite']['slug'] = 'place'; } return $args; } add_filter( 'register_post_type_args', '...


7

I found the answer on Wordpress' website and have tested the solution: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule#Using_Custom_Templates_with_custom_querystring Create a Page Template Create a page template for stores and apply it to a page. $stores = get_query_var('stores'); Add a Rewrite Tag In your functions.php add a rewrite tag, which ...


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