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 Create New Post Types and Taxonomies. This is the script:

add_action('init', 'my_custom_post_type_init');
function my_custom_post_type_init()
{
  $labels = array(
    'name' => _x('Roundabouts', 'post type general name'),
    'singular_name' => _x('Roundabout', 'post type singular name'),
    'add_new' => _x('Add New', 'Roundabout'),
    'add_new_item' => __('Add New Roundabout'),
    'edit_item' => __('Edit Roundabout'),
    'new_item' => __('New Roundabout'),
    'view_item' => __('View Roundabout'),
    'search_items' => __('Search Roundabouts'),
    'not_found' =>  __('No Roundabouts found'),
    'not_found_in_trash' => __('No Roundabouts found in Trash'),
    'parent_item_colon' => ''
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 5,
    'rewrite' => array('slug' => '%category%', 'with_front' => FALSE),
    'taxonomies' => array( 'post_tag', 'category'),
    'supports' => array('title','editor','author','thumbnail','excerpt','comments', 'thumbnail')
  );
  register_post_type('roundabouts',$args);
}

and this is the permalink setting which I am not allowed to change.

/%category%/%postname%/

Now I required to generate permalink like:

http://my_site/category_name/post_title

while google I found many articles, issues:

but almost waste 4 hours of my life then after complete R&D at last I found

which somehow convert my post permalink to as I require but if I hit that link I get the 404 error.

Here is the modified script which is best suitable with my above code:

// Add filter to plugin init function
add_filter('post_type_link', 'gallery_permalink', 10, 3);  
// Adapted from get_permalink function in wp-includes/link-template.php
function gallery_permalink($permalink, $post_id, $leavename) {
    $post = get_post($post_id);

    $rewritecode = array(
        '%year%',
        '%monthnum%',
        '%day%',
        '%hour%',
        '%minute%',
        '%second%',
        $leavename? '' : '%postname%',
        '%post_id%',
        '%category%',
        '%author%',
        $leavename? '' : '%pagename%',
    );

    if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {

        $unixtime = strtotime($post->post_date);

        $category = '';
        if ( strpos($permalink, '%category%') !== false ) {

            $cats = get_the_category($post->ID);
            if ( $cats ) {
                usort($cats, '_usort_terms_by_ID'); // order by ID
                $category = $cats[0]->slug;
                if ( $parent = $cats[0]->parent )
                    $category = get_category_parents($parent, false, '/', true) . $category;
            }
            // show default category in permalinks, without
            // having to assign it explicitly
            if ( empty($category) ) {
                $default_category = get_category( get_option( 'default_category' ) );
                $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
            }
        }

        $author = '';
        if ( strpos($permalink, '%author%') !== false ) {
            $authordata = get_userdata($post->post_author);
            $author = $authordata->user_nicename;
        }

        $date = explode(" ",date('Y m d H i s', $unixtime));
        $rewritereplace =
        array(
            $date[0],
            $date[1],
            $date[2],
            $date[3],
            $date[4],
            $date[5],
            $post->post_name,
            $post->ID,
            $category,
            $author,
            $post->post_name,
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    } else { // if they're not using the fancy permalink option
    }
    return $permalink;
}

Please guide me what additional I could do.

share|improve this question

2 Answers 2

I can't comment on your scripts, but if you change anything related to permalinks - it is always advisable to go to the Dashboard -> Settings -> Permalinks and click the "Update" button. This tells Wordpress to flush its rewrite rules. If your script is correct, then after doing the "Update" - you won't get 404 errors.

share|improve this answer

Thanks to sanchothefat for this plugin it really solve my problem.

WP Permastructure

share|improve this answer
    
Please mark your answer as correct using the green tick –  bungeshea_ May 19 '13 at 5:56

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.