0

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();
}
  • 2
    I noticed you're flushing rewrite rules on the 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 Jun 12 at 15:52
  • Hi Tom, thanks for the answer! I did not know that would cause so many issues. Unfortunately removing it does not address the issue. I removed it and then manually went and pressed save changes under permalink settings just to be sure. Still no change – James Jun 12 at 16:53
0

The 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.

  • HI Jacob, thanks for responding. Unfortunately removing that did not help address the issue. I even manually flushed the permalinks after just in case, still nothing. Any other ideas what could be causing this? – James Jun 12 at 16:55

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.