Title: Changing permalink, using postid instead of postname sample code
Last modified: August 20, 2016

---

# Changing permalink, using postid instead of postname sample code

 *  [takuya](https://wordpress.org/support/users/takuya/)
 * (@takuya)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/changing-permalink-using-postid-instead-of-postname-sample-code/)
 * I spent more than a few hours to find and get this working (wish I knew programming).
   This example code below lets you change the permalink from ai1ec_event to event,
   and also allows displaying `%post_id%` instead of `%postname%`.
 * Add this to your functions.php and don’t forget to click save on your WordPress
   permalink setting page (you don’t have to change anything).
 *     ```
       add_action('init', 'ai1ec_event_rewrite');
       function ai1ec_event_rewrite() {
           global $wp_rewrite;
       	$queryarg = 'post_type=ai1ec_event&p=';
           $wp_rewrite->add_rewrite_tag('%ai1ec_event_id%', '([^/]+)',$queryarg);
           $wp_rewrite->add_permastruct('ai1ec_event', '/event/%ai1ec_event_id%', false); }
   
       add_filter('post_type_link', 'ai1ec_event_permalink', 1, 3);
       function ai1ec_event_permalink($post_link, $id = 0, $leavename) {
           global $wp_rewrite;
           $post = &get_post($id);
           if ( is_wp_error( $post ) )
       	return $post;
           $newlink = $wp_rewrite->get_extra_permastruct($post->post_type);
           $newlink = str_replace('%'.$post->post_type.'_id%', $post->ID, $newlink);
           $newlink = home_url(user_trailingslashit($newlink));
           return $newlink;
       }
       ```
   
 * [http://wordpress.org/extend/plugins/all-in-one-event-calendar/](http://wordpress.org/extend/plugins/all-in-one-event-calendar/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [asdevargas](https://wordpress.org/support/users/asdevargas/)
 * (@asdevargas)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/changing-permalink-using-postid-instead-of-postname-sample-code/#post-3208032)
 * Hello! Thank you for this tip, it works great but I need something a little bit
   different, and definitely, my PHP knowledge are short: I tried to change things
   in your code but always get errors. In the slug, instead of “event” I would like
   to have “calendari” and keep the post name (instead of having id). Any help would
   be very appreciate!! Thank you in advance
 *  [Valentina Giorcelli](https://wordpress.org/support/users/webgardens/)
 * (@webgardens)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/changing-permalink-using-postid-instead-of-postname-sample-code/#post-3208033)
 * same here, I’ like to keep the post name.
 * So far I’ve found this hack, I do not particularly like it as it changes core
   files, however it is the only way I’ve found that lets me change the permalink
   and keep the post name
 * [http://wordimpress.com/all-in-one-event-calendar-custom-permalink-slugs/](http://wordimpress.com/all-in-one-event-calendar-custom-permalink-slugs/)
 * Hope it helps

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Changing permalink, using postid instead of postname sample code’ is 
closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/all-in-one-event-calendar.svg)
 * [Timely All-in-One Events Calendar](https://wordpress.org/plugins/all-in-one-event-calendar/)
 * [Support Threads](https://wordpress.org/support/plugin/all-in-one-event-calendar/)
 * [Active Topics](https://wordpress.org/support/plugin/all-in-one-event-calendar/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/all-in-one-event-calendar/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/all-in-one-event-calendar/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [Valentina Giorcelli](https://wordpress.org/support/users/webgardens/)
 * Last activity: [12 years, 10 months ago](https://wordpress.org/support/topic/changing-permalink-using-postid-instead-of-postname-sample-code/#post-3208033)
 * Status: not a support question