Custom Post Type Permalink
Tue Apr 22 2025 11:00:31 GMT+0000 (Coordinated Universal Time)
Saved by @Bh@e_LoG
// Register Custom Post Type function custom_ourproducts_post_type() { $labels = array( 'name' => _x( 'Our Products', 'Post Type General Name', 'text_domain' ), 'singular_name' => _x( 'Our Product', 'Post Type Singular Name', 'text_domain' ), 'menu_name' => __( 'Our Products', 'text_domain' ), 'name_admin_bar' => __( 'Our Product', 'text_domain' ), 'archives' => __( 'Our Product Archives', 'text_domain' ), 'attributes' => __( 'Our Product Attributes', 'text_domain' ), 'parent_item_colon' => __( 'Parent Product:', 'text_domain' ), 'all_items' => __( 'All Products', 'text_domain' ), 'add_new_item' => __( 'Add New Product', 'text_domain' ), 'add_new' => __( 'Add New', 'text_domain' ), 'new_item' => __( 'New Product', 'text_domain' ), 'edit_item' => __( 'Edit Product', 'text_domain' ), 'update_item' => __( 'Update Product', 'text_domain' ), 'view_item' => __( 'View Product', 'text_domain' ), 'view_items' => __( 'View Products', 'text_domain' ), 'search_items' => __( 'Search Product', 'text_domain' ), 'not_found' => __( 'Not found', 'text_domain' ), 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), 'featured_image' => __( 'Featured Image', 'text_domain' ), 'set_featured_image' => __( 'Set featured image', 'text_domain' ), 'remove_featured_image' => __( 'Remove featured image', 'text_domain' ), 'use_featured_image' => __( 'Use as featured image', 'text_domain' ), 'insert_into_item' => __( 'Insert into product', 'text_domain' ), 'uploaded_to_this_item' => __( 'Uploaded to this product', 'text_domain' ), 'items_list' => __( 'Products list', 'text_domain' ), 'items_list_navigation' => __( 'Products list navigation', 'text_domain' ), 'filter_items_list' => __( 'Filter products list', 'text_domain' ), ); $args = array( 'label' => __( 'Our Product', 'text_domain' ), 'description' => __( 'Custom post type for our products', 'text_domain' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'thumbnail' ), 'taxonomies' => array( 'product_category' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 15, 'menu_icon' => 'dashicons-cart', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'post', 'rewrite' => array( 'slug' => '%product_category%', 'with_front' => false ), ); register_post_type( 'ourproducts', $args ); } add_action( 'init', 'custom_ourproducts_post_type', 0 ); // Register Custom Taxonomy function custom_product_taxonomy() { $labels = array( 'name' => _x( 'Product Categories', 'Taxonomy General Name', 'text_domain' ), 'singular_name' => _x( 'Product Category', 'Taxonomy Singular Name', 'text_domain' ), 'menu_name' => __( 'Product Categories', 'text_domain' ), 'all_items' => __( 'All Categories', 'text_domain' ), 'parent_item' => __( 'Parent Category', 'text_domain' ), 'parent_item_colon' => __( 'Parent Category:', 'text_domain' ), 'new_item_name' => __( 'New Category Name', 'text_domain' ), 'add_new_item' => __( 'Add New Category', 'text_domain' ), 'edit_item' => __( 'Edit Category', 'text_domain' ), 'update_item' => __( 'Update Category', 'text_domain' ), 'view_item' => __( 'View Category', 'text_domain' ), 'separate_items_with_commas' => __( 'Separate categories with commas', 'text_domain' ), 'add_or_remove_items' => __( 'Add or remove categories', 'text_domain' ), 'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ), 'popular_items' => __( 'Popular Categories', 'text_domain' ), 'search_items' => __( 'Search Categories', 'text_domain' ), 'not_found' => __( 'Not Found', 'text_domain' ), 'no_terms' => __( 'No categories', 'text_domain' ), 'items_list' => __( 'Categories list', 'text_domain' ), 'items_list_navigation' => __( 'Categories list navigation', 'text_domain' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_tagcloud' => true, 'rewrite' => array( 'slug' => 'category', 'with_front' => false, 'hierarchical' => true ) ); register_taxonomy( 'product_category', array( 'ourproducts' ), $args ); } add_action( 'init', 'custom_product_taxonomy', 0 ); // Filter the permalink to include taxonomy slug function filter_ourproducts_permalink($post_link, $post) { if ( $post->post_type === 'ourproducts' ) { $terms = wp_get_post_terms($post->ID, 'product_category'); if ( ! empty($terms) && ! is_wp_error($terms) ) { return str_replace('%product_category%', $terms[0]->slug, $post_link); } else { return str_replace('%product_category%', 'uncategorized', $post_link); } } return $post_link; } add_filter( 'post_type_link', 'filter_ourproducts_permalink', 10, 2 );
Comments