views:

27

answers:

1

I am building a new WordPress theme using register_post_type to create a custom post type called Listings.

I would like each listing to have the following permalink structure:

http://www.mysite.com/post-author/listing-title

I'm not quite sure how to achieve this with the CPT controls listed in the Codex. Any help or links would be greatly appreciated!

A: 

Actually, you'll need to define your own custom URL rewrite rule to achieve this ... it's not a built-in feature of custom posts. There's a good example in the Codex that will get you started.

Here's a quick untested first-thought of how you could do this:

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['([^/]+)/([^/]+)$'] = 'index.php?author=$matches[1]&listing-title=$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'listing-title');
    return $vars;
}

This should direct any links from http://www.mysite.com/me/my-listing to http://www.mysite.com/index.php?author=me&listing-title=my-listing. Handling the query after WordPress gets ahold of it is entirely up to you.

EAMann
Thanks, reading up on WP_Rewrite...
trnsfrmr
Wow! This is quite tricky. Going to take me a while to figure this out...
trnsfrmr
OK, your answer looks like it can only handle custom requests, whereas I'm trying to change the permalink of custom post type posts. I will add an update to my question to better explain what I'm trying to do.
trnsfrmr
Actually. I'll re-post this in the WP forum...
trnsfrmr
Re-posted with more details over here: http://wordpress.stackexchange.com/questions/3151/in-wordpress-how-do-you-create-a-custom-post-type-with-the-authors-name-in-the-s
trnsfrmr