I've been having trouble lately in trying to figure out why this custom post type is not using the correct template file. The custom post type is called 'job' and it has a rewrite on it for the slug to be 'careers'. Any time I make a new job posting I get brought to our blog page, but the url path will be correct. I installed a plugin to see what template was currently showing and it's displaying our home.php template.
The weird thing is that if I go back into the job post and change the permalink to anything else, even just removing the trailing '/' the link will work correctly and pull the correct page template of 'single-job'.
I also noticed that if I remove the slug rewriting WordPress will also use the correct template. Is anyone able to provide any advice on what could be happening here and how to fix it? I have tried flushing my rewrite rules as well, no luck.
Here's my code below:
add_action('init', 'create_custom_post_types');
function create_custom_post_types() {
$job_labels = array(
'name' => _x( 'Jobs', 'Post Type General Name', 'textdomain' ),
'singular_name' => _x( 'Job', 'Post Type Singular Name', 'textdomain' ),
'menu_name' => _x( 'Jobs', 'Admin Menu text', 'textdomain' ),
'name_admin_bar' => _x( 'Job', 'Add New on Toolbar', 'textdomain' ),
'archives' => __( 'Job Archives', 'textdomain' ),
'attributes' => __( 'Job Attributes', 'textdomain' ),
'parent_item_colon' => __( 'Parent Job:', 'textdomain' ),
'all_items' => __( 'All Jobs', 'textdomain' ),
'add_new_item' => __( 'Add New Job', 'textdomain' ),
'add_new' => __( 'Add New', 'textdomain' ),
'new_item' => __( 'New Job', 'textdomain' ),
'edit_item' => __( 'Edit Job', 'textdomain' ),
'update_item' => __( 'Update Job', 'textdomain' ),
'view_item' => __( 'View Job', 'textdomain' ),
'view_items' => __( 'View Jobs', 'textdomain' ),
'search_items' => __( 'Search Job', 'textdomain' ),
'not_found' => __( 'Not found', 'textdomain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
'featured_image' => __( 'Featured Image', 'textdomain' ),
'set_featured_image' => __( 'Set featured image', 'textdomain' ),
'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
'insert_into_item' => __( 'Insert into Job', 'textdomain' ),
'uploaded_to_this_item' => __( 'Uploaded to this Job', 'textdomain' ),
'items_list' => __( 'Jobs list', 'textdomain' ),
'items_list_navigation' => __( 'Jobs list navigation', 'textdomain' ),
'filter_items_list' => __( 'Filter Jobs list', 'textdomain' ),
);
$rewrite = array(
'slug' => 'careers',
'with_front' => false,
'pages' => true,
'feeds' => true,
);
$job_args = array(
'label' => __( 'Job', 'textdomain' ),
'description' => __( '', 'textdomain' ),
'labels' => $job_labels,
'menu_icon' => 'dashicons-category',
'supports' => array('title', 'editor', 'excerpt'),
'taxonomies' => array(),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'hierarchical' => true,
'exclude_from_search' => false,
'show_in_rest' => true,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => $rewrite,
);
register_post_type( 'job' , $job_args );
flush_rewrite_rules();
}
I've been having trouble lately in trying to figure out why this custom post type is not using the correct template file. The custom post type is called 'job' and it has a rewrite on it for the slug to be 'careers'. Any time I make a new job posting I get brought to our blog page, but the url path will be correct. I installed a plugin to see what template was currently showing and it's displaying our home.php template.
The weird thing is that if I go back into the job post and change the permalink to anything else, even just removing the trailing '/' the link will work correctly and pull the correct page template of 'single-job'.
I also noticed that if I remove the slug rewriting WordPress will also use the correct template. Is anyone able to provide any advice on what could be happening here and how to fix it? I have tried flushing my rewrite rules as well, no luck.
Here's my code below:
add_action('init', 'create_custom_post_types');
function create_custom_post_types() {
$job_labels = array(
'name' => _x( 'Jobs', 'Post Type General Name', 'textdomain' ),
'singular_name' => _x( 'Job', 'Post Type Singular Name', 'textdomain' ),
'menu_name' => _x( 'Jobs', 'Admin Menu text', 'textdomain' ),
'name_admin_bar' => _x( 'Job', 'Add New on Toolbar', 'textdomain' ),
'archives' => __( 'Job Archives', 'textdomain' ),
'attributes' => __( 'Job Attributes', 'textdomain' ),
'parent_item_colon' => __( 'Parent Job:', 'textdomain' ),
'all_items' => __( 'All Jobs', 'textdomain' ),
'add_new_item' => __( 'Add New Job', 'textdomain' ),
'add_new' => __( 'Add New', 'textdomain' ),
'new_item' => __( 'New Job', 'textdomain' ),
'edit_item' => __( 'Edit Job', 'textdomain' ),
'update_item' => __( 'Update Job', 'textdomain' ),
'view_item' => __( 'View Job', 'textdomain' ),
'view_items' => __( 'View Jobs', 'textdomain' ),
'search_items' => __( 'Search Job', 'textdomain' ),
'not_found' => __( 'Not found', 'textdomain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
'featured_image' => __( 'Featured Image', 'textdomain' ),
'set_featured_image' => __( 'Set featured image', 'textdomain' ),
'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
'insert_into_item' => __( 'Insert into Job', 'textdomain' ),
'uploaded_to_this_item' => __( 'Uploaded to this Job', 'textdomain' ),
'items_list' => __( 'Jobs list', 'textdomain' ),
'items_list_navigation' => __( 'Jobs list navigation', 'textdomain' ),
'filter_items_list' => __( 'Filter Jobs list', 'textdomain' ),
);
$rewrite = array(
'slug' => 'careers',
'with_front' => false,
'pages' => true,
'feeds' => true,
);
$job_args = array(
'label' => __( 'Job', 'textdomain' ),
'description' => __( '', 'textdomain' ),
'labels' => $job_labels,
'menu_icon' => 'dashicons-category',
'supports' => array('title', 'editor', 'excerpt'),
'taxonomies' => array(),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'hierarchical' => true,
'exclude_from_search' => false,
'show_in_rest' => true,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => $rewrite,
);
register_post_type( 'job' , $job_args );
flush_rewrite_rules();
}
Share
Improve this question
edited Jun 12, 2019 at 15:51
Tom J Nowell♦
61.2k7 gold badges79 silver badges150 bronze badges
asked Jun 12, 2019 at 15:47
JamesJames
1
2
|
1 Answer
Reset to default 0The problem is caused by flush_rewrite_rules()
. Remove that line and the issue should be resolved.
If you call that function on every page load it can cause issues like this with permalinks. This function should only be run once after your theme or plugin is activated (but not on the activation hook, since that's too early). This is because permalinks are persistent and should only be flushed if something has changed.
It's often easier to just manually flush the permalinks by visiting Settings > Permalinks.
init
hook, this is bad practice, is a very expensive thing to do, and can cause issues for some plugins and code, does the problem go away if you remove it? – Tom J Nowell ♦ Commented Jun 12, 2019 at 15:52