0

I am currently having an issue with my single custom post types returning a 404 error. The thing that is baffling me is that I have set this up exactly the same for another custom post type and it works perfectly. Although it was a good few hours of trial and error and searching forum posts to achieve this for my other custom post type - so I'm not sure if there was something else that I had done that has now been deleted but still helped with setting it up correctly?

What I am trying to achieve is this permalink structure for my custom post type to be

  • 'news-media' - archive page
  • 'news-media/custom-taxonomy' - taxonomy archive page
  • 'news-media/custom-taxonomy/name-of-post' - single post page

Previously I had this set up as

  • 'news-media' - archive page (which is correct)
  • 'news-media/custom-taxonomy' - taxonomy archive page (which is correct)
  • 'news-media-article/name-of-post'- single post page (this one is incorrect)

Here is my code:

add_action( 'init', 'canvas_cpt_init' );

function canvas_cpt_init() {
    $labels = array(
        'name'               => _x( 'News Media', 'post type general name', 'canvas' ),
        'singular_name'      => _x( 'News Media', 'post type singular name', 'canvas' ),
        'menu_name'          => _x( 'News & Media', 'admin menu', 'canvas' ),
        'name_admin_bar'     => _x( 'News & Media', 'add new on admin bar', 'canvas' ),
        'add_new'            => _x( 'Add New', 'News Media', 'canvas' ),
        'add_new_item'       => __( 'Add New News Media', 'canvas' ),
        'new_item'           => __( 'New News Media', 'canvas' ),
        'edit_item'          => __( 'Edit News Media', 'canvas' ),
        'view_item'          => __( 'View News Media', 'canvas' ),
        'all_items'          => __( 'All News & Media', 'canvas' ),
        'search_items'       => __( 'Search News & Media', 'canvas' ),
        'parent_item_colon'  => __( 'Parent News Media:', 'canvas' ),
        'not_found'          => __( 'No News & Media found.', 'canvas' ),
        'not_found_in_trash' => __( 'No News & Media found in Trash.', 'canvas' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'news-media/%mediacat%', 'with_front' => false ),
        'capability_type'    => 'post',
        'has_archive'        => 'news-media',
        'hierarchical'       => true,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author','thumbnail')
    );

    register_post_type( 'newsmedia', $args );

    $labels = array(
        'name'              => __('Categories','canvas'),
        'singular_name'     => __('Category','canvas'),
        'search_items'      => __('Search Categories','canvas'),
        'all_items'         => __( 'All Categories', 'canvas' ),
        'edit_item'         => __( 'Edit Category', 'canvas' ),
        'update_item'       => __( 'Update Category', 'canvas' ),
        'add_new_item'      => __( 'Add New Category', 'canvas' ),
        'new_item_name'     => __( 'New Category Name', 'canvas' ),
        'menu_name'         => __( 'News Category', 'canvas' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'public'            => true,
        'has_archive'       => true,
        'rewrite'           => array( 'with_front' => false, 'slug' => 'news-media' ),
    );
    register_taxonomy( 'news-media', array( 'newsmedia' ), $args );
}

function wpa_show_permalinks_media( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'newsmedia' ){
            $terms = wp_get_object_terms( $post->ID, 'news-media' );
            if( $terms ){
                    return str_replace( '%mediacat%' , $terms[0]->slug , $post_link );
            }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks_media', 10, 2 );

Edit I should also mention that I have re-saved the permalinks in the admin dashboard. (On numerous occasions) and still receive a 404 error. I have also tried swapping the 'has_archive' from true, false and 'news-media' for both the register_post_type and register_taxonomy.

0

So after some more research I came across this https://stackoverflow.com/questions/23698827/custom-permalink-structure-custom-post-type-custom-taxonomy-post-name - which I now think I remember using at some point but must have deleted thinking it hadn't worked (as I hadn't figured out the rest of the process). But this is the particular function and filter that worked:

/**
 * Tell WordPress how to interpret our project URL structure
 *
 * @param array $rules Existing rewrite rules
 * @return array
 */
function so23698827_add_rewrite_rules( $rules ) {
  $new = array();
  $new['news-media/([^/]+)/(.+)/?$'] = 'index.php?newsmedia=$matches[2]';
  $new['news-media/(.+)/?$'] = 'index.php?news-media=$matches[1]';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );

| improve this answer | |

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.