2 years ago
#69512
PXD
How to add 2 different taxonomies to Wordpress CPT permalink structure
I have problem to add a second taxonomy to my cpt permalink structure.
With one register_taxonomy (pxlr-products-category) everything works fine and looks like this: https://example.com/products/my_category/my_productname
But I want a second register_taxonomy (pxlr-products-status). When I do that I see in my permalink instead of the tax slug just this %status% https://example.com/products/my_category/%status%/my_productname and I get an 404 error. Of cause I'ved flushed my permalinks
A second thing is, (pxlr-products-status) can have a value like (discontinued) but it is not a must, it can be empty as well
My permalinks should look like this https://example.com/products/my_category/my_status/my_productname
Or look like this https://example.com/products/my_category/my_productname
Here is my CPT, I can't find my error and need help, thanks :-/
class PXLR_Products
{
function __construct(){
// register post type
add_action('init', array($this, 'register_products_post_type'));
// register taxonomy
add_action('init', array($this, 'register_products_taxonomy'));
// slug - category filter
add_filter('post_type_link', [$this,'products_post_link'], 1, 3 );
// Rewrite support
add_action('init', [$this, 'custom_rewrite_rules']);
}
function custom_rewrite_rules() {
add_rewrite_rule(
'products/(.+?)/?$',
'index.php?pxlr-products-category=$matches[1]',
'bottom');
// add_rewrite_rule(
// 'products/(.+?)/(.+?)/?$',
// 'index.php?pxlr-products-category=$matches[1]&pxlr-products-status=$matches[2]',
// 'bottom');
}
public function register_products_post_type()
{
$labels = array(
'name' => _x('Products', 'Post Type General Name', 'pxlr-products'),
'singular_name' => _x('Products', 'Post Type Singular Name', 'pxlr-products'),
'menu_name' => __('PXLR Products', 'pxlr-products'),
'all_items' => __('All Products', 'pxlr-products'),
'view_item' => __('Show product', 'pxlr-products'),
'add_new_item' => __('Add product', 'pxlr-products'),
'add_new' => __('Add Product', 'pxlr-products'),
'edit_item' => __('Edit product', 'pxlr-products'),
'update_item' => __('Update product', 'pxlr-products'),
'search_items' => __('Search products', 'pxlr-products'),
'not_found' => __('No products found', 'pxlr-products'),
'not_found_in_trash' => __('No products found in trash.', 'pxlr-products'),
);
$args = array(
'labels' => $labels,
'name' => 'Products',
'name_admin_bar' => 'Products',
'singular_name' => 'Products',
'menu_icon' => 'dashicons-microphone',
'label' => __('Products', 'pxlr-products'),
'hierarchical' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'show_in_nav_menus' => false,
'menu_position' => 28,
'supports' => array('title', 'editor', 'revisions', 'thumbnail', 'page-attributes', 'excerpt', 'custom-fields'),
'rest_base' => 'products',
'public' => true,
'show_ui' => true,
'has_archive' => 'products',
'can_export' => false,
'taxonomies' => array('pxlr-products-category', 'pxlr-products-status'),
'publicaly_queryable' => true,
'query_var' => true,
'exclude_from_search' => false,
//'rewrite' => ['slug' => _x('products/%category%', 'Post Type Slug', 'pxlr-products'), 'with_front' => true ], // for one taxonomy if working fine!!!!
'rewrite' => ['slug' => _x('products/%category%/%status%', 'Post Type Slug', 'pxlr-products'), 'with_front' => true ], // for two taxonomies
'template_lock' => 'all',
);
register_post_type('pxlr-products', $args);
}
public function products_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'pxlr-products-category' );
if( $terms ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
}
$terms = wp_get_object_terms( $post->ID, 'pxlr-products-status' );
if( $terms ){
return str_replace( '%status%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
public function register_products_taxonomy()
{
register_taxonomy(
'pxlr-products-category',
'pxlr-products',
array(
'label' => __('Categories Product', 'pxlr-products'),
'hierarchical' => true,
'with_front' => false,
'show_in_rest' => true,
'rewrite'=> ['slug' => _x('products', 'Category Slug', 'pxlr-products'),
'with_front' => false,
'hierarchical' => true
],
'show_admin_column' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
)
);
register_taxonomy(
'pxlr-products-status',
'pxlr-products',
array(
'label' => __('Status Product', 'pxlr-products'),
'hierarchical' => true,
'with_front' => false,
'show_in_rest' => true,
'rewrite'=> ['slug' => _x('products/%category%', 'Status Slug', 'pxlr-products'),
'with_front' => false,
'hierarchical' => true
],
'show_admin_column' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
)
);
}
}
wordpress
url
custom-post-type
permalinks
custom-taxonomy
0 Answers
Your Answer