0

I've created a custom post type ('careers') in a plugin. I can create and view individual career posts just fine, but I can't view the archive page. It just redirects to the home page.

The following code creates the post type

function create_career_posttype() {
  $args = array(
    'labels' => array(
      'name' => __('Careers'),
      'singular_name' => __('Careers'),
      'all_items' => __('All Job Postings'),
      'add_new_item' => __('Add New Job Posting'),
      'edit_item' => __('Edit Job Posting'),
      'view_item' => __('View Job Posting')
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'careers'),
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_admin_bar' => true,
    'capability_type' => 'post',
    'supports' => array('title', 'editor', 'thumbnail'),
    'exclude_from_search' => true,
    'menu_position' => 6,
    'has_archive' => true,
    'menu_icon' => 'dashicons-universal-access'
    );
  register_post_type('careers', $args);
}

I've added a menu item for all the job postings by adding "all job postings" but it redirects to the home page for some reason. I also have an archive-careers.php defined. According to my (weak) understanding it should have a permalink of myurl/careers isn't that right?

I said I broke it in the subject because this used to work. Between that time and this I moved all the code from functions.php to a plugin with a great amount of pain (working Custom Post Type and Widget code no longer works when moved from functions.php to plugin), and at one point I had some code in funtions to hide all category and archive pages which I have since removed and restarted the web server numerous times. That code looked like this:

/* Register template redirect action callback */
add_action('template_redirect', 'meks_remove_wp_archives');

/* Remove archives */
function meks_remove_wp_archives(){
  //If we are on category or tag or date or author archive
  if( is_category() || is_tag() || is_date() || is_author() ) {
    global $wp_query;
    $wp_query->set_404(); //set to 404 not found page
  }
}
10
  • 2
    To troubleshoot I would deactivate the plugin (you won't lose your content), reactivate it, then go to Settings > Permalinks and save without making changes. This will flush your permalinks and may solve the problem.
    – WebElaine
    Feb 28 '17 at 19:33
  • Two things I can think of: 1, what hook are you using for create_career_posttype()? It would need to most likely be init. 2, try going into your Wordpress Settings and going to permalinks. Just try hitting "Save Changes". It might refresh your permalink settings. This (for some reason) has worked for me before.
    – Samyer
    Feb 28 '17 at 19:33
  • @Samyer all of the code is in the other question I linked to. It's ``` add_action( 'init', 'create_career_posttype');```
    – marcp
    Feb 28 '17 at 20:00
  • @WebElaine, yes I've tried that (several times)
    – marcp
    Feb 28 '17 at 20:05
  • How about deregistering the CPT altogether and re-registering it? If you changed anything about permalink structure along the way WP may not be using your latest code.
    – WebElaine
    Feb 28 '17 at 20:10
0

This is usually due to the rewrite rules not being updated. You need to run flush_rewrite_rules() or visit the Permalinks page in the WP-Admin and save it to flush the rewrite rules. If you use the function call in your plugin, be sure not to leave it in such that it gets run every time, it's not friendly that way.

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.