0

I've defined a custom post type, 'Review', that I'd like to utilize with a custom permalink: /reviews/year/postname. The below code correctly creates the CPT and the custom permalink appears correctly in the administrative panel when creating a new 'Review'. However, the resulting 'Review' that is created returns a 404 when clicking the custom permalink. What is the modern wordpress way of solving this issue? Ideally I'd like not not use any external plugins. The below code is defined within a custom plugin.

// Register 'Review' custom post type
add_action( 'init', 'vidcreate_post_types' );

// Register the permalink structure desired for 'Review' posts, with placeholders
add_action( 'init', 'vidrewrite_rules' );

// Replace permalink placeholders with actual content
add_filter('post_type_link', 'update_review_link_placeholders', 10, 2);



/**
 * Custom 'Review' post type
 */
function vidcreate_post_types() {

    register_post_type( 'review',
        array(
            'labels' => array(
                'name' => __( 'Reviews' ),
                'singular_name' => __( 'Review' )
            ),
            'public' =>  true,
            'has_archive' => true,
            'taxonomies' => array('category', 'post_tag'),   
            'rewrite' => array(
                'slug'=>'reviews',
                'with_front'=> false,
                'feed'=> true,
                'pages'=> true
            ),
            'hierarchical' => true,
            'menu_icon' => 'dashicons-video-alt',
            'supports' => array(
                'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions', 'page-attributes'
            )
        )
    );


}


/**
 * Create custom permalink structure for 'Review' pages
 */
function vidrewrite_rules(){

    add_rewrite_tag( '%review_slug%', '(reviews)','post_type=review&slug=' );
    add_permastruct( 'review', 'reviews/%year%/%review%');

}


/**
 * Update permalinks placeholders with content
 */
function update_review_link_placeholders($permalink, $post) {
    if(('review' == $post->post_type) && '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {

        // currently just replacing %year % with 2014 for testing
        $permalink = str_replace('%year%', '2014', $permalink);

        return $permalink;
    }


}
  • 1
    Save once permalink and then try again. – Bindiya Patoliya Dec 29 '14 at 4:41
1

In your register_post_type, you set

'with_front'=> false,

Later you declare the following function without arguments:

add_permastruct( 'review', 'reviews/%year%/%review%');

add_permastruct has an argument of with_front and the default value is true, which conflicts with your earlier declaration.

  • 1
    This was exactly the problem, that you very much! – ph34r Dec 29 '14 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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