plugins - I broke it! Custom post type archive gets redirected to home

I've created a custom post type ('careers') in a plugin. I can create and view individual career posts ju

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
  }
}

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
  }
}
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Feb 28, 2017 at 19:27 marcpmarcp 1235 bronze badges 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 Commented Feb 28, 2017 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 Commented Feb 28, 2017 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 Commented Feb 28, 2017 at 20:00
  • @WebElaine, yes I've tried that (several times) – marcp Commented Feb 28, 2017 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 Commented Feb 28, 2017 at 20:10
 |  Show 5 more comments

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744348627a4569841.html

相关推荐

  • plugins - I broke it! Custom post type archive gets redirected to home

    I've created a custom post type ('careers') in a plugin. I can create and view individual career posts ju

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信