Shiba

Adventures in WordPress

  • — Home —
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Admin / Custom Post Type / Custom Post Type Permalinks

Custom Post Type Permalinks

Tweet

by ShibaShake 10 Comments

Custom post types is a very powerful feature in WordPress 3.0. With custom post types you can easily create post-like objects and automatically have menus, metaboxes, icons, permalinks, and much more added without having to code any of it yourself.

Here we consider custom post type permalinks – how to set it, what the different options mean, and how each of them affects your custom post type permalink structure.

In Custom Post Type Permalinks- Part 2, we consider how to flexibly modify the permalinks to something other than the given defaults. This article gives a more in-depth explanation of WordPress permalinks in general.

In the examples below, we assume our new custom post type is called gallery.

Register Post Type Permalink Options

Permalink behavior can be easily defined in our call to register_post_type. The options in register_post_type that relate to permalinks include –

  1. publicly_queryable
  2. query_var
  3. rewrite
  4. permalink_epmask

1. publicly_queryable

Defines whether users can query our custom post type from the main blog page. For example, if we set publicly_queryable to true, we can enter –

http://shibashake.com/wordpress-theme?gallery=test-1

This will show the gallery object that has the slug test-1. If publicly_queryable is set to false, then users will get a 404 or file not found error page instead.

2. query_var

Defines the name used in our public query above. query_var defaults to our custom post type name – e.g., gallery.

We can also set our query_var to be something else. For example, if we set the query_var argument to ‘foo’, then our gallery object query will look like this –

http://shibashake.com/wordpress-theme?foo=test-1

gallery=test-1 will no longer work because we have set our query_var to foo.

3. rewrite

Rewrite will add pretty permalink rules for our new custom post type. For example, if we set rewrite to true, then we can access our gallery objects with the following link –

http://shibashake.com/wordpress-theme/gallery/test-1

We can further specify two additional arguments in our rewrite argument –

  • slug
  • with_front
'rewrite' => array('slug' => 'shiba-gallery', 'with_front' => FALSE)

This means our gallery pretty permalink will now become –

http://shibashake.com/wordpress-theme/shiba-gallery/test-1

with_front determines whether or not to prepend our blog permalink structure in front of our custom post type object. If we set with_front to true and our blog permalink structure is –

/%year%/%monthnum%/%postname%

Then both of the following permalinks will show our gallery object –

http://shibashake.com/wordpress-theme/shiba-gallery/test-1
http://shibashake.com/wordpress-theme/2010/06/shiba-gallery/test-1

4. permalink_epmask

Permalink epmask is a bit more complex, and need not be used in most cases.

epmask stands for endpoint mask.

An endpoint is something that is added to the end of an object permalink. For example, /trackback/ is an endpoint.

Different endpoints are attached to different epmasks using the add_rewrite_endpoint command.

epmasks indicate which type of links an endpoint (e.g. /trackback/) should be added to. epmasks may contain the following values or combinations of the following values (for example if we want an endpoint to be added into several different types of links).

  • EP_NONE
  • EP_PERMALINK
  • EP_ATTACHMENT
  • EP_DATE
  • EP_YEAR
  • EP_MONTH
  • EP_DAY
  • EP_ROOT
  • EP_COMMENTS
  • EP_SEARCH
  • EP_CATEGORIES
  • EP_TAGS
  • EP_AUTHORS
  • EP_PAGES
  • EP_ALL

The permalink_epmask argument allows us to specify which group(s) of endpoints we want to add to our custom post type permalinks. For example, if we set permalink_epmask to EP_PAGES & EP_TAGS then we will associate all endpoints that are linked to pages and tags with our custom object permalinks.

The permalink_epmask argument defaults to EP_PERMALINK. This is the link type for regular posts. I.e., the same endpoints being added to posts will also get added to our new custom post type.

If we do not want to add any endpoints to our new gallery object, then we want to set permalink_epmask to EP_NONE.

If we want to add all endpoints to our new gallery object, then we want to set permalink_epmask to EP_ALL.

Flush Permalink Rules

Note – If you change permalink settings, you may need to flush your previous rewrite rules before the new changes will take effect.

To do this, you want to add the flush_rules function call to your plugin activation and deactivation function.

// add to your plugin deactivation function
global $wp_rewrite;
$wp_rewrite->flush_rules();

Custom Post Type Permalinks- Part 2 explains how we can fully modify our entire custom post type permalink structure.

Related Articles

Custom Post Type Permalinks - Part 2

How to fully customize and create our own permalinks for custom post type objects.

Add Custom Taxonomy Tags to Your WordPress Permalinks

How to include taxonomy tags into our custom post type permalinks.

Add a Metabox to Your Custom Post Type Screen

How to add our own metabox to the edit custom post type screen.

Comments

  1. yogendra sharma says

    August 12, 2016 at 6:34 am

    I need to know that how do i set permalink for selected category/tag/post. I want to use google news sitemap for one of my entertainment site yetket.com without changing the old permalink for all posts.

    Reply
  2. BenRacicot says

    July 25, 2013 at 12:44 pm

    Great article. Big questions though. I was able to get my first CPT to pretty URLs but my second one 404’s on the page. The perms save correct in edit.php… I CANNOT understand why at this point.

    Reply
    • ShibaShake says

      July 25, 2013 at 2:25 pm

      Sounds like it could be because of permalink conflicts-
      http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict

      Reply
  3. Rico Leuthold says

    March 26, 2013 at 4:12 am

    Excellent – ‘with_front’ => FALSE is exactly what I was looking for. Thanks!

    Reply
  4. chris says

    April 15, 2012 at 2:29 pm

    hey, great tutorials. works well to change the permalink, however with the new ones set up i just get a 404 error. The post edit page shows the new permalink but when I open it, 404 error still.

    is there anything i might be missing?

    thanks 🙂

    Reply
    • ShibaShake says

      April 15, 2012 at 6:26 pm

      Sounds like it could be a permalink conflict –
      http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict

      Reply
  5. Michael says

    February 28, 2012 at 2:30 am

    Thanks for this article, helped me fix up the permalinks on my custom post types.

    Reply
  6. Kirsy says

    August 10, 2010 at 1:13 am

    Hi There! Thanks for the post. I’m kind of a newby with this stuff. Can you tell me where (which .php?) i need to ad this:

    global $wp_rewrite;
    $wp_rewrite->flush_rules();

    Hope you can help me out..
    Tnx! Kirsy

    Reply
    • ShibaShake says

      August 11, 2010 at 10:16 am

      I usually flush the rewrite rules on my deactivation function – i.e. the function tied to my plugin deactivation hook.

      register_deactivation_hook( __FILE__, 'my_deactivate_function'  );
      
      function my_deactivate_function() {
          	global $wp_rewrite;
      	$wp_rewrite->flush_rules();	
      }
      
      Reply

Leave a Reply Cancel reply

Your email address will not be published.

Recent Posts

  • Screen-shot of mobile responsive Poll Daddy object, where text floats properly to the right of radio buttons.How to Make Poll Daddy Objects Mobile Responsive
  • Screen-shot of blog post with no page border (flowing design).Genesis Skins 1.5
  • Screen-shot of the media manager Create-Gallery screen, while doing a post search.Shiba Media Library 3.7
  • Screenshot of the Edit Gallery screen after we hit the Create Gallery button.How to Expand the WordPress Media Manager Interface
  • Blonde girl looking through and holding a circular picture frame.Shiba Gallery 4.3
  • Close-up of beautiful blonde holding a square picture frame.Google Authorship - Good or Bad for Search Traffic?
  • Shiba Widgets 2.0
  • Media Temple screenshot of editing my sub-domain DNS entry.Using CDN Cnames with w3tc and MultiSite
  • Shiba Skins WordPress ThemeShiba Skins WordPress Theme
  • How to add the Media Manager Menu to the Theme Preview InterfaceHow to Add the Media Manager Menu to the Theme Preview Interface

Recent Comments

  • WordPress Search Widget – How to Style It (56)
    • Nelson
      - Tanks master - Fine
    • TelFiRE
      - How do you style the "X" that shows up when you start typing?
  • Update custom inputs with the proper data using Javascript.Expand the WordPress Quick Edit Menu (58)
    • Mike G
      - This is exactly what is happening to me. It is updating the value in the database and in the column, but I have to refresh ...
    • PhoenixJP
      - Thanks for this tutorial. Does someone knows if this still work with wordpress 5.03 and 5.1.
    • Francine Carrel
      - This is a very long time away from your original comment, but did you ever work it out? I am stuck on the exact same thing ...
  • Custom meta-box with a set of radio-buttons.Add a Metabox to Your Custom Post Type Screen (27)
    • mike
      - Hi Shiba am a newbie to wordpress, I just installed a plugin with a custom post type, but has no option for discussion and ...
  • Write a Plugin for WordPress Multi-Site (45)
    • Andrew
      - Hi,action 'wpmu_new_blog' has been deprecated. Use ‘wp_insert_site’ instead.
  • Populate our Edit Gallery menu using a gallery shortcode.How to Add the WordPress 3.5 Media Manager Interface – Part 2 (29)
    • Janine
      - Still relevant in 2019.
  • WordPress Excerpt – How to Style It (36)
    • James
      - Great post. I really need some help. I have set border lines around my excerpts on my blog page/post page. The problem is ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (123)
    • Darshan Saroya
      - Update permalinks. Go to settings > permalink and just save it.

Copyright © 2021 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·