Title: Get post name from permalink
Last modified: August 19, 2016

---

# Get post name from permalink

 *  Resolved [wccoder](https://wordpress.org/support/users/wccoder/)
 * (@wccoder)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/get-post-name-from-permalink/)
 * I’m writing a plugin that passes information to a post or page via URI segments.
   I have added a URI segment that I use to differentiate between the permalink 
   for the post/page and the parameters I am passing. For example:
 * [http://foo.bar/permalink/X/param1/param2](http://foo.bar/permalink/X/param1/param2)
 * I have added rewrite rules for pages with no issue, however posts are giving 
   me a problem. The reason is I don’t know how to extract the post name from the
   permalink definition to generate a proper regex. Something like:
 * ‘(.+?)/X/(.*)’ => ‘index.php?name=’$wp_rewrite->preg_index(1).’&params=’.$wp_rewrite-
   >preg_index(2);
 * Works great for the permalink structure of /%postname% but will obviously break
   if I use ‘/%category%/%postname%’ since the name query_var will be passed the
   category as well and will 404. So, based on a permalink definition does WordPress
   have a way of giving me a regex that will result in the post name? Or is there
   a better way of doing this, such as somehow stripping off the extra parameters
   higher up in the action chain?

Viewing 1 replies (of 1 total)

 *  Thread Starter [wccoder](https://wordpress.org/support/users/wccoder/)
 * (@wccoder)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/get-post-name-from-permalink/#post-1697103)
 * This was resolved by writing code to generate a regex based on the permalink 
   setting and stripping out the position of the %postname% parameter.

Viewing 1 replies (of 1 total)

The topic ‘Get post name from permalink’ is closed to new replies.

## Tags

 * [permalink](https://wordpress.org/support/topic-tag/permalink/)
 * [query_vars](https://wordpress.org/support/topic-tag/query_vars/)

 * 1 reply
 * 1 participant
 * Last reply from: [wccoder](https://wordpress.org/support/users/wccoder/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/get-post-name-from-permalink/#post-1697103)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
