Search code examples
wordpresscustom-post-typepermalinks

custom WordPress permalink structure not changed for all custom post types


I changed the default permalink structure in my WordPress site to this: www.example.com/blog/%postname%/. I want the '/blog/' part to apply only to posts. I have custom post types like 'education', 'case-study', and 'resource' where I don't want 'blog' to be part of the url. I added a rewrite argument to the register_post_type function like so: 'rewrite' => array('slug' => $slug , 'with_front' => false). It worked for all the custom post types except for 'resource'. I added code to flush the rewrite rules and I reset the permalinks without any success. Is there something I am missing?

SOLUTION (UPDATED): Here is the code where I register my custom post types:

/** Remove extraneous resource custom post type */
add_action('init','delete_post_type', 5);
function delete_post_type(){
  unregister_post_type( 'resource' );
}

/** Register custom post types */
add_action( 'init', 'add_custom_register_post_types', 99 );
function add_custom_register_post_types() {    
  custom_register_post_type( 'Resource', 'Resources', 'resource', 'resources', true, array( 'title', 'editor', 'thumbnail' ), true, array( 'category' ) );
  custom_register_post_type( 'Case Study', 'Case Studies', 'case_study', 'case-studies', true, array( 'title', 'editor', 'thumbnail', ), true, array( 'category', 'post_tag' ) );
  custom_register_post_type( 'Product', 'Products', 'product', 'product', true, array( 'title', 'thumbnail' ), true, array(), false );
  custom_register_post_type( 'Guest Speaker', 'Guest Speakers', 'guest-speaker', 'guest-speaker', false, array( 'title', 'editor', 'thumbnail' ), true, array(), false );
  custom_register_post_type( 'News and Media Post', 'News and Media', 'news-and-media', 'company/news-and-media', true, array( 'title', 'editor' ), true, array() );
  custom_register_post_type( 'Education', 'Education', 'education', 'education1', true, array( 'title', 'editor', 'thumbnail', 'custom-fields' ), true, array( 'category', 'post_tag' ) );

  //flush_rewrite_rules();
}

function custom_register_post_type( $type, $types, $cpt_name, $slug, $search=false, $supports, $showui=true, $taxonomies, $has_archive = true ){

  $type2=strtolower( $type );
  $types2=strtolower( $types );

  $labels = array(
    'name' => $type,
    'singular_name' => $type,
    'add_new' => 'Add New',
    'add_new_item' => 'Add New ' .$type,
    'edit_item' => 'Edit '.$type,
    'new_item' => 'New '.$type,
    'view_item' => 'View '.$type,
    'search_items' => 'Search '.$types,
    'not_found' =>  'No '.$types2.' found',
    'not_found_in_trash' => 'No '.$types2.' found in Trash',
    'parent_item_colon' => '',
    'menu_name' => $types
  );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => $showui,
        'query_var' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'has_archive' => $has_archive,
        'rewrite' => array('slug' => $slug , 'with_front' => false),
        'exclude_from_search' => ! $search,
        'supports' => $supports,
        'taxonomies' => $taxonomies
    );

  register_post_type( $cpt_name, $args );
}

Solution

  • I wasn't able to figure out where the other place the resource custom post type was being registered but I found a hack that solved my problem. I added an unregister function and set the action priority higher than the register function. I updated my code to reflect the solution. I know it's not best practice but I didn't want to spend forever trying to figure out where in the other place in the code the resource custom post type was being created.