Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Numerical permalinks for post type WordPress

  • SOLVED

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

Answers (6)

2011-10-14

Luis Abarca answers:

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


Matt Taylor comments:

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


Luis Abarca comments:

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


Luis Abarca comments:

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


Luis Abarca comments:

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.


Matt Taylor comments:

Thank worked. Thanks Luis!


Matt Taylor comments:

*That

2011-10-14

Luis Cordova answers:

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

2011-10-14

Fahad Murtaza answers:

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


Matt Taylor comments:

Can you post an example?

2011-10-14

Sébastien | French WordpressDesigner answers:

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


Matt Taylor comments:

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

2011-10-14

Abdessamad Idrissi answers:

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 ),
));


Abdessamad Idrissi comments:

[[LINK href="http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/#comment-1005"]]Check this out[[/LINK]] the solution is to use a filter : add_filter('post_type_link', 'my_post_type_link_filter_function');


Matt Taylor comments:

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


Abdessamad Idrissi comments:

You don't need a taxonomy, but rather a filter to change the permalink.
<blockquote>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.</blockquote>

Did you try the link I posted above?

2011-10-14

Milan Petrovic answers:

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/