Single page not working for custom post type

I created a custom post type with the following code:

add_action('init', 'create_trans_career');

function create_trans_career() {
    //Arguments to create post type.
    $args = array(
        'labels' = array(
            'name' = __('Career', 'trans'),
            'singular_name' = __('Career', 'trans'),
            'add_new' = __('Add New', 'trans'),
            'add_new_item' = __('Add New Career Item', 'trans'),
            'edit' = __('Edit', 'trans'),
            'edit_item' = __('Edit Career Item', 'trans'),
            'new_item' = __('New Career', 'trans'),
            'view' = __('View', 'trans'),
            'view_item' = __('View Career', 'trans'),
            'search_items' = __('Search Career', 'trans'),
            'not_found' = __('No Career item found', 'trans'),
            'not_found_in_trash' = __('No Career item found in Trash', 'trans'),
        ),
        'public' = true,
        'show_ui' = true,
        'capability_type' = 'post',
        'hierarchical' = true,
        'has_archive' = true,
        'menu_icon' = get_bloginfo('template_directory'). '/images/career-icon.png',
        'supports' = array('title', 'editor', 'thumbnail'),
        'rewrite' = array('slug' = 'career', 'with_front' = false),
    );

    //Register type and custom taxonomy for type.
    register_post_type( 'career' , $args );
}

I also created a single page (single-career.php):

?php while ( have_posts() ) : the_post(); ?
            h2 class="page-title"?php the_title(); ?/h2
            hr /
            ?php the_content('Read More...'); ?
            a href="?php bloginfo('wpurl'); ?/career" title="?php _e('Back to jobs listings', 'trans'); ?" class="back-to-jobs"?php _e('Back to jobs listings', 'trans'); ?/a
            ?php endwhile;  ?

..and a page template to list the posts:

$mypost = array( 'post_type' = 'career', 'posts_per_page' = 10, 'paged' = $pagedNum );
            $loop = new WP_Query( $mypost ); ?
            ?php while ( $loop-have_posts() ) : $loop-the_post();?
            tr id="post-?php the_ID(); ?" ?php post_class(); ?
                td?php the_title(); ?/td
                td?php echo $location; ?/td
                td?php echo $close_date; ?/td
                tda href="?php the_permalink(); ?" title="?php _e('Details', 'trans'); ?"?php _e('gt; Details', 'trans'); ?/a/td
                td class="apply-now"a data-title="?php the_title(); ?" data-location="?php echo $location; ?" href="#apply_?php the_ID(); ?" title="?php _e('Apply Now', 'trans'); ?" class="career-apply"?php _e('Apply Now', 'trans'); ?/a/td
            /tr
            ?php endwhile;  ?

I created a page with jobs using the template above.. it's working I see all posts.. but when I open a single page I receive 404 error. Where is the problem?

Topic single custom-post-types Wordpress

Category Web


@welbyaudry solution works but the process is a bit lengthy. I want to make it simpler for others. Just place the below code in the functions.php of your active theme and refresh your page.

/* Flush rewrite rules for custom post types. */
add_action( 'init', 'bt_flush_rewrite_rules' );

/* Flush your rewrite rules */
function bt_flush_rewrite_rules() {
     flush_rewrite_rules();
}

Thanks


It seems like there are lots of different problems and potential solutions here, but after trying every other suggested solution, none of which worked, I found something that worked. Add:

flush_rewrite_rules();

after you call register_post_type.


Add this to your functions.php:

/* Flush rewrite rules for custom post types. */
add_action( 'after_switch_theme', 'bt_flush_rewrite_rules' );

/* Flush your rewrite rules */
function bt_flush_rewrite_rules() {
     flush_rewrite_rules();
}

Then change your theme to a different theme and then set it back again to your custom theme. This will flush the rewrite rule and making the custom post working properly.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.