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.

For all posts, regardless of of the post type I want the URL to be, /read/%post_id%/%postname% and have the custom post types be archives so you can go to /%post_type% and /%post_type%/%category%.

My permalinks is set to /read/%post_id%/%postname%. This is what I've tried to get working so far. It's not pulling the post ID yet but I can't get this to work for the post or the archive.

add_action('init', 'create_post_type_columns');
function create_post_type_columns() {
    register_taxonomy_for_object_type('category', 'columns');
    register_taxonomy_for_object_type('post_tag', 'columns');
    register_post_type('columns',
        array(
        'labels' => array(
            'name' => 'Columns',
            'singular_name' => 'Column',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Column',
            'edit' => 'Edit',
            'edit_item' => 'Edit Column',
            'new_item' => 'New Column',
            'view' => 'View Column',
            'view_item' => 'View Column',
            'search_items' => 'Search Columns',
            'not_found' => 'No columns found',
            'not_found_in_trash' => 'No columns found in Trash'
        ),
        'public' => true,
        'menu_position' => 4,
        'hierarchical' => true,
        'has_archive' => true,
        'rewrite' => array(
            'slug' => 'read',
            'with_front' => true),
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail'
        ),
        'can_export' => true,
        'taxonomies' => array(
            'post_tag',
            'category'
        )
    ));
}

add_action('init', 'columns_rewrite');
function columns_rewrite() {
    global $wp_rewrite;
    $queryarg = 'post_type=columns&p=';
    $wp_rewrite->add_rewrite_tag('%cpt_id%', '([^/]+)', $queryarg);
    $wp_rewrite->add_permastruct('columns', '/read/%cpt_entry%/%cpt_id%', false);
}

add_filter('post_type_link', 'columns_permalink', 1, 3);
function columns_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('columns');
    $newlink = str_replace("%cpt_id%", $post->ID, $newlink);
    $newlink = str_replace("%cpt_entry%", $post->post_name, $newlink);
    $newlink = home_url(user_trailingslashit($newlink));
    return $newlink;
}
share|improve this question
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.