Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having a really difficult time with this. I have a multi-user gallery site. Each author has his own page that lists the author's posts (this is a custom post type).

I have made it so that the author url(author.php) is www.example.com/firstname-lastname/. But when a visitor clicks on one of the author's posts, the url changes to www.example.com/custom-post-type-name/postname/ while I want it to be www.example.com/firstname-lastname/postname/. How do I achieve this? With .htaccess? Or changing the rewrite rule when registering the post type? How?

I use this to register my post type in functions.php file:

// Make custom post type Add media

function galleryRegister()
{
$labels = array(
'name' => _x('Add gallery', 'post type general name'),
'singular_name' => _x('Add gallery', 'post type singular name'),
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' => false,
'show_in_nav_menus' => true,
);

register_post_type('gallery' , $args);
}

add_action('init', 'galleryRegister');

Thanks for help.

Edit: I found out that I can manually type www.example.com/firstname-lastname/postname/ and it shows the correct post, but the permalink is still same old www.example.com/custom-post-type-name/postname/ everywhere

Solution: Thank you Stephen for the great answer! This is the code that I used and it worked:

// ****************************************************
// Make a custom post type "Add gallery"
// ****************************************************
function galleryRegister()
{
$labels = array(
'name' => _x('Add gallery', 'post type general name'),
'singular_name' => _x('Add gallery', 'post type singular name'),
'add_new' => _x('Add gallery ', 'portfolio item'),
'all_items' => __( 'Manage your galleries' ),
'add_new_item' => __('Add gallery'),
'edit_item' => __('Edit your galleries'),
'new_item' => __('New gallery'),
'view_item' => __('View gallery on site'),
'search_items' => __('Search galleries'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' =>  array( 'slug' => _x( '%author%', 'URL slug') ),
'show_in_nav_menus' => true,
);

register_post_type('gallery' , $args);
}

add_action('init', 'galleryRegister');

// ****************************************************
// Flush rewrite rules. Delete this 
// ****************************************************
function my_rewrite_flush() {
    my_cpt_init();

    // ATTENTION: This is *only* done during plugin activation hook in this example!
    // You should *NEVER EVER* do this on every page load!!
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_rewrite_flush' );

// ****************************************************
// Make author as slug for posts
// ****************************************************
add_filter('post_type_link', 'wpse73228_author_tag',10,4);
function wpse73228_author_tag($post_link, $post, $leavename, $sample){

    if( 'gallery' != get_post_type($post) )
        return $post_link;

    $authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;

$post_link = str_replace('%author%', $author, $post_link);

return $post_link;
}
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You can use the %author% tag in the rewrite property in register_post_type(). However, although the rewrite rules are added (after they're flushed) - WordPress doesn't replace the tag with its appropriate value when generating the permalink of your post type. For instance you end up with the permalink www.example.com/%author%/gallery-name

The following replaces %author% with the appropriate value:

add_filter('post_type_link', 'wpse73228_author_tag',10,4);
function wpse73228_author_tag($post_link, $post, $leavename, $sample){

    if( 'gallery' != get_post_type($post) )
        return $post_link;

    $authordata = get_userdata($post->post_author);
    $author = $authordata->user_nicename;

    $post_link = str_replace('%author%', $author, $post_link);

    return $post_link;
}
share|improve this answer
    
Thank you very much! It worked perfectly. –  hlotvonen Nov 20 '12 at 19:08
1  
This worked well for my CPTs, but now clicking any of my non-CPTs results in a 404! I posted more details at wordpress.stackexchange.com/q/124253/28957 and would appreciate if anyone has any guidance. –  cfx Nov 23 '13 at 20:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.