0

I am registering a custom post type through this function in a plugin. There is a problem that the end url of this post type has a slug. I want to remove that slug and replace it with the category of that post.

So there are two things I am trying to do.

  1. Remove CPT slug.
  2. Add CPT category to the URL.

e.g

www.abc.com/product/samsung-galaxy-note

should be

www.abc.com/custposttype-category/samsung-galaxy-note

How can I do this?

public static function register_cpt_aps_products() {
        $permalinks = get_aps_settings('permalinks');
        $slug = (isset($permalinks['product-slug'])) ? $permalinks['product-slug'] : '';

    // labels text for our post type aps-products
    $labels = array(
        // post type general name
        'name' => __( 'APS Products', 'aps-text' ),
        // post type singular name
        'singular_name' => __( 'APS Product', 'aps-text' ),
        'name_admin_bar' => __( 'APS Product', 'aps-text' ),
        'menu_name' => __( 'APS Products', 'aps-text' ),
        'add_new' => __( 'Add New APS Product', 'aps-text' ),
        'add_new_item' => __( 'Add New APS Product', 'aps-text' ),
        'edit_item' => __( 'Edit APS Product', 'aps-text' ),
        'new_item' => __( 'New APS Product', 'aps-text' ),
        'view_item' => __( 'View APS Product', 'aps-text' ),
        'archives' => __( 'APS Products Archives', 'aps-text' ),
        'search_items' => __( 'Search APS Products', 'aps-text' ),
        'insert_into_item' => __( 'Insert into APS Product', 'aps-text' ),
        'featured_image' => __( 'APS Product Image', 'aps-text' ),
        'set_featured_image' => __( 'Set APS Product Image', 'aps-text' ),
        'remove_featured_image' => __( 'Remove APS Product Image', 'aps-text' ),
        'use_featured_image' => __( 'Use as APS Product image', 'aps-text' ),
        'not_found' =>  __( 'No APS Products found', 'aps-text' ),
        'not_found_in_trash' => __( 'No APS Products found in Trash', 'aps-text' )
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'show_ui' => true,
        'query_var' => true,
        'publicly_queryable' => true,
        'show_in_nav_menus' => false,
        'menu_icon' => 'dashicons-products',
        'capability_type' => 'aps-products',
        'capabilities' => array(
           'read_post' => 'read_aps_product',
           'edit_post' => 'edit_aps_product',
           'edit_posts' => 'edit_aps_products',
           'delete_posts' => 'delete_aps_products',
           'create_posts' => 'create_aps_products',
           'publish_posts' => 'publish_aps_products',
           'edit_published_posts' => 'edit_published_aps_products',
           'delete_published_posts' => 'delete_published_aps_products',
           'edit_others_posts' => 'edit_others_aps_products',
           'delete_others_posts' => 'delete_others_aps_products',
           'read_private_posts' => 'read_private_aps_products',
           'edit_private_posts' => 'edit_private_aps_products',
           'delete_private_posts' => 'delete_private_aps_products'
        ),
        'map_meta_cap' => true,
        'hierarchical' => false,
        'taxonomies' => array('post_tag','aps-cats', 'aps-brands', 'aps-attributes', 'aps-filters', 'aps-rating-bars'),
        'has_archive' => true,
        'show_in_menu' => 'aps-products',
        'supports' => array( 'publicize','title', 'editor', 'thumbnail', 'comments', 'author', 'excerpt' ),
        'register_meta_box_cb' => array(__CLASS__, 'add_aps_products_metabox'),
        'rewrite' => array('slug' => $slug, 'with_front' => false)
    );

    $args = apply_filters('cpt_aps_products_args', $args);
    register_post_type( 'aps-products', $args );
}
  • Possible duplicate of Add category base to url in custom post type/taxonomy – Anton Lukin Aug 24 '17 at 18:50
  • I have tried following that answer. It is not working for me. 1. I want to remove CPT slug. 2. I want to add custom post type category in URL. – Rookie Aug 24 '17 at 19:08
  • Hmm. Is custom post type category = custom post type taxonomy? – Anton Lukin Aug 24 '17 at 19:13
  • Yes, I think so: 'taxonomies' => array('post_tag','aps-cats', 'aps-brands', 'aps-attributes', 'aps-filters', 'aps-rating-bars'), I want aps-cats to appear as slug. – Rookie Aug 24 '17 at 19:16
  • Take a look at this article, please shibashake.com/wordpress-theme/… – Anton Lukin Aug 24 '17 at 19:21
-1

Use This...

function chapter_post_type_init() {
        $labels = array(
                'name' => 'Chapters',
                'singular_name' => 'Chapter',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Chapter',
                'edit_item' => 'Edit Chapter',
                'new_item' => 'New Chapter',
                'all_items' => 'All Chapters',
                'view_item' => 'View Chapter',
                'search_items' => 'Search Chapters',
                'not_found' =>  'No chapters found',
                'not_found_in_trash' => 'No chapters found in Trash',
                'parent_item_colon' => '',
                'menu_name' => 'Books'
        );

        $args = array(
                'labels' => $labels,
                'public' => true,
                'publicly_queryable' => true,
                'show_ui' => true,
                'show_in_menu' => true,
                'taxonomies'   => array( 'category', 'post_tag' ),
                'query_var' => true,
                'rewrite' => array( 'slug' => '/%category%','with_front' => false,'hierarchical' => true),
                'capability_type' => 'post',
                'has_archive' => true,
                'hierarchical' => false,
                'menu_position' => 100,
                'supports' => array( 'title', 'editor', 'author', 'excerpt')
        );
        register_post_type( 'chapter', $args );
}
add_action( 'init', 'chapter_post_type_init' );


add_filter('post_type_link', 'book_permalink_structure', 10, 6);
function book_permalink_structure($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, '%category%' ) ) {
        if( $book_type_term = wp_get_object_terms( $post->ID, 'category' ) )
        {
            if($book_type_term[1]->parent == 0 ){
             $post_link = str_replace( '%category%', array_pop( $book_type_term )->slug, $post_link );              
            }else{
        $post_link = str_replace( '%category%', $book_type_term[0]->slug.'/'.$book_type_term[1]->slug, $post_link );
            }
        }
    }
    return $post_link;
}
|improve this answer|||||
  • Your post_type_link function throws a bunch of warnings with debugging enabled. Also removing the post type slug this way breaks any parent/child pages of the page post type, causing them to 404. – Milo Sep 14 '18 at 15:16

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.