logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Numerical permalinks for post type

I need a function that will give a custom post type %post_id% permalinks. All other post types on the site use %postname%.

This question has been answered.

Matt Taylor | 10/14/11 at 9:17am Edit


(6) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/14/11
    9:22am
    Luis Cordova says:

    for custom post type %post_id% permalinks you perhaps just need the guid field

  • avatar
    Last edited:
    10/14/11
    9:22am
    Fahd Murtaza says:

    You can use 'slug' parameter of register_post_type() function in order to do that.

    • 10/14/11 9:37am

      Matt Taylor says:

      Can you post an example?

  • avatar
    Last edited:
    10/14/11
    9:23am
    Sébastien | French WordpressDesigner says:

    customize your url for this CPT :
    http://shibashake.com/wordpress-theme/custom-post-type-permalinks

    Previous versions of this answer: 10/14/11 at 9:23am | 10/14/11 at 9:23am

    • 10/14/11 9:38am

      Matt Taylor says:

      I'm looking for a working function to be supplied here :)

  • avatar
    Last edited:
    10/14/11
    9:28am
    Abdessamad Idrissi says:

    Normally you do that when you register your custom post type.
    something like :


    $labels = array(
    'name' => __( 'Product Categories', 'taxonomy general name' )
    );
    register_taxonomy('portfolio_cats',array('portfolio_posts'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'gallery', 'with_front'=>false ),
    ));

    • 10/14/11 9:36am

      Abdessamad Idrissi says:

      Check this out the solution is to use a filter :

      add_filter('post_type_link', 'my_post_type_link_filter_function');

    • 10/14/11 10:05am

      Matt Taylor says:

      I don't need a taxonomy to be in the permalink.

    • 10/14/11 10:18am

      Abdessamad Idrissi says:

      You don't need a taxonomy, but rather a filter to change the permalink.

      When you create a new post type, you use the register_post_type function. When you create a new post type through that function, WP creates a new rewrite rule base on the name you give the post type. In my case, I’ve created an issue post type, so all issues, by default, will have the permalink /issue/%issue%/ (where %issue% is replaced with the post slug). Using the rewrite argument to register_post_type, you can change the beginning of the permalink to something different (e.g., /bugs/%issue%/) should you so desire.


      Did you try the link I posted above?

  • avatar
    Last edited:
    10/14/11
    9:44am
    Luis Abarca says:

    Change your slug to:



    add_filter('post_type_link', 'cpt_type_link', 1, 3);

    function cpt_type_link($url, $post = null, $leavename = false)
    {
    $tag = '%post_id%';

    if ($post->post_type != 'mycpt') {
    return $url;
    }

    $post_id = $post->ID;

    // replace tag with the slug: /youcpt/1234
    return str_replace($tag, $post_id, $url);
    }



    function cpt_add_rewrite_rules()
    {
    register_post_type('mycpt', array(
    'rewrite' => array('slug' => 'products/%post_id%'),
    'query_var' => true // ....
    )
    );
    flush_rewrite_rules(); // call just on plugin activation
    }

    add_action('init', 'cpt_add_rewrite_rules');

    Previous versions of this answer: 10/14/11 at 9:39am | 10/14/11 at 9:42am | 10/14/11 at 9:44am

    • 10/14/11 10:03am

      Matt Taylor says:

      That results in a url of "http://website.com/album/%post_id/first-post"

    • 10/14/11 10:43am

      Luis Abarca says:

      Update:


      add_filter('post_type_link', 'cpt_type_link', 1, 3);

      function cpt_type_link($url, $post = null, $leavename = false)
      {
      $tag = '%post_id%';

      if ($post->post_type != 'mycpt') {
      return $url;
      }

      $post_id = $post->ID;

      // replace tag with the slug: /youcpt/1234
      return str_replace($tag, $post_id, $url);
      }


      function cpt_add_rewrite_rules()
      {
      global $wp_rewrite;

      register_post_type('mycpt', array(
      'name' => 'My Custom type',
      'label' => 'Slug as ID',
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'products/%post_id%'),
      'query_var' => true // ....
      )
      );

      $wp_rewrite->extra_permastructs['mycpt'][0] = "/products/%post_id%";

      add_rewrite_rule("products/([0-9]{1,})/?$", 'index.php?post_type=mycpt&mycpy=$matches[1]', 'top');
      add_rewrite_rule("products/([0-9]{1,})/page/([0-9]{1,})/?$", 'index.php?post_type=mycpt&mycpy=$matches[1]&paged=$matches[2]', 'bottom');

      flush_rewrite_rules(); // call just on plugin activation
      }

      add_action('init', 'cpt_add_rewrite_rules');

    • 10/14/11 10:43am

      Luis Abarca says:

      Its working here http://dev.justoalblanco.com/products/37/

    • 10/14/11 10:55am

      Luis Abarca says:

      The above code have some bugs, check use this one

      http://pastebin.com/PiJXVenN

      Its working here
      http://dev.justoalblanco.com/products
      http://dev.justoalblanco.com/products/38/

      Let me know if you have any issue.

    • 10/14/11 2:12pm

      Matt Taylor says:

      Thank worked. Thanks Luis!

    • 10/14/11 2:12pm

      Matt Taylor says:

      *That

  • avatar
    Last edited:
    10/14/11
    9:40am
    Milan Petrovic says:

    My plugin GD Custom Posts and Taxonomies Tools Pro allows you to set custom permalinks for single post and archives for custom post types with great degree of flexibility, just like you can do for default posts:

    http://www.dev4press.com/2011/tutorials/plugins/gd-taxonomies-tools/enhanced-custom-post-types-url-rewriting/
    http://www.dev4press.com/2011/tutorials/plugins/gd-taxonomies-tools/custom-permalinks-for-custom-post-types/
    http://www.dev4press.com/gd-custom-posts-and-taxonomies-tools/

This question has expired.



Matt Taylor voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.