Post ID instead of the slug in the Friendly URL for the post type – “post_type_/ID”
Suppose, when registering the post type orders
using register_post_type() parameter rewrite = false
- the rewrite rules are not created and must be generated manually.
The URL should look like example.com/orders/123
, where 123 is the post ID.
add_action( 'init', function(){ // Create friendly URL rules add_rewrite_tag( '%order_id%', '([0-9]+)', "post_type=orders&p=" ); // or you can do it this way // create CNC rules - here 'order/([0-9]+)/?$' is exactly // the same as what add_permastruct() would create and so it replaces... //add_filter( 'orders'.'_rewrite_rules', function( $rules ){ // return array( 'order/([0-9]+)/?$' => 'index.php?post_type=orders&p=$matches[2]' ); //} ); // register the structure for the post type add_permastruct( 'orders', 'order/%order_id%', array( 'with_front' => false, 'paged' => false, 'feed' => false, 'forcomments' => false, 'walk_dirs' => false, 'endpoints' => false, ) ); // Replace the slug in the URL with an ID add_filter( 'post_type_link', 'orders_permalink', 1, 2 ); function orders_permalink( $permalink, $post ){ if( false === strpos( $permalink, '%order_id%' ) ){ return $permalink; } return str_replace( '%order_id%', $post->ID, $permalink ); } } );
—
This Note embeded into: WP_Rewrite{}