Custom Post Type – Category Rewrite – Remove Rewrite from Sitemap

Question

I’ve got “case_studies” post type and categories for it as “case_studies_categories” with a rewrite to include category in URL.

Everything works, but for some reason, rewrite url is in sitemap (as the first URL), for example:

/case-studies/%case_studies_categories%/

and the rest is fine:

/case-studies/%case_studies_categories%/
/case-studies/category-name/post-name/
/case-studies/category-name/post-name/
/case-studies/category-name/post-name/

How to remove it (/case-studies/%case_studies_categories%/) from Sitemap?

    add_action('init','case_studies_init');

function case_studies_init(){

    $labels = array(
        'name'               => _x( 'Case Studies', 'Case Studies' ),
        'singular_name'      => _x( 'Case Study', 'Case Study' ),
        'add_new'            => _x( 'Add Case Study', 'Case Study' ),
        'add_new_item'       => __( 'Add Case Study' ),
        'edit_item'          => __( 'Edit Case Study' ),
        'new_item'           => __( 'New Case Study' ),
        'all_items'          => __( 'All Case Study' ),
        'view_item'          => __( 'View Case Study' ),
        'search_items'       => __( 'Search Case Study' ),
        'not_found'          => __( 'No Case Studies Found' ),
        'not_found_in_trash' => __( 'No Case Studies in Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Case Studies'
    );
    $args = array(
        'labels'                => $labels,
        'description'           => 'Holds case studies post data',
        'public'                => true,
        'menu_position'         => 7,
        'hierarchical'          => true,
        'menu_icon'             => 'dashicons-admin-comments',
        'rewrite'               => array('slug' => 'case-studies/%case_studies_categories%', 'with_front' => false),
        'supports'              => array( 'title', 'revisions', 'thumbnail'),
        'has_archive'           => true,
        'show_ui'               => true,
        'show_in_nav_menus'     => true,
        'show_in_menu'          => true,
        'show_in_admin_bar'     => true,
        'taxonomies'            => array("case_study_categories"),
    );

    register_post_type('case_studies',$args);
  //  flush_rewrite_rules( false );
}

// register a custom category taxonomy type
// so that the categories are not connected to the 'post' type taxonomies

add_action( 'init', 'register_case_study_tax' );

function register_case_study_tax(){

    $labels = array(
    'name'              => _x( 'Case Study Categories', 'case-studies'),
    'singular_name'     => _x( 'Case Study Category', 'testimonials'),
    'search_items'      => __( 'Search Case Study Categories'),
    'all_items'         => __( 'All Case Study Categories'),
    'parent_item'       => __( 'Parent Case Study Category'),
    'parent_item_colon' => __( 'Parent Case Study Category:'),
    'edit_item'         => __( 'Edit Case Study Category'),
    'update_item'       => __( 'Update Case Study Category'),
    'add_new_item'      => __( 'Add Case Study Category'),
    'new_item_name'     => __( 'New Case Study Category'),
    'menu_name'         => __( 'Case Study Categories'),
    );

    $args = array(
    'labels'                => $labels,
    'taxonomy'              => 'case_study_categories',
    'object_type'           => 'case_studies',
    'hierarchical'          => true,
    'show_ui'               => true,
    'show_admin_column'     => true,
    'query_var'             => false,
    );

    register_taxonomy('case_studies_categories', 'case_studies', $args);
}


/** filter URL link for post type url **/
add_filter('post_type_link', 'case_studies_permalink_structure', 10, 4);

function case_studies_permalink_structure($post_link, $post, $leavename, $sample) {
  if ( false !== strpos( $post_link, '%case_studies_categories%' ) ) {
    $event_type_term = get_the_terms( $post->ID, 'case_studies_categories' );
    if($event_type_term)
    $post_link = str_replace( '%case_studies_categories%', array_pop( $event_type_term )->slug, $post_link );
   }
 return $post_link;
}

I’m sure I’ve done something stupid, please assist if possible.

Thanks,

0
, , , Pavel 2 years 2019-11-24T17:49:25-05:00 0 Answers 77 views 0

Leave an answer

Browse
Browse