Remove CPT Slug with 2 taxonomies and 1 post meta

I am trying to remove my CPT slug I have tried various answers on stack overflow but none seem to work for some reason.

My Code:

add_filter(‘post_type_link’, array(__CLASS__, ‘rewrite_permalink’), 10, 3);

//Tell WP products can rewrtie this way
add_action( ‘pre_get_posts’, array(__CLASS__, ‘products_parse_request’ ) );

add_action(‘init’, array( __CLASS__, ‘add_rewrite_tags’), 10, 0);

public static function add_rewrite_tags() {
add_rewrite_tag(‘%product_holiday%’, ‘([^&]+)’);
}

function rewrite_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
if ( self::$post_type != $post->post_type || ‘publish’ != $post->post_status ) return $permalink;

// Get post
if (!$post) return $permalink;

// Get country terms
$countries = wp_get_object_terms($post->ID, ‘_product_countries’);
if (!is_wp_error($countries) && !empty($countries) && is_object($countries[0])) {
$country_slug = $countries[0]->slug;
} else {
$country_slug = ‘country’;
}

//Get product type
$product_type = wp_get_object_terms($post->ID, ‘_product_types’);
if (!is_wp_error($product_type) && !empty($product_type) && is_object($product_type[0])) {
$product_type_slug = $product_type[0]->slug;
} else {
$product_type_slug = ‘product-type’;
}

//Get post holiday
$holiday = get_post_meta($post->ID, ‘_event_holiday_type’, true);
if (!empty($holiday)) {
$holiday = $holiday;
} else {
$holiday = ‘holiday’;
}

return strtr($permalink, array( ‘%product_holiday%’ => $holiday, ‘%_product_countries%’ => $country_slug, ‘%_product_types%’ => $product_type_slug));

//’/’ . self::$post_type . ‘/’ => ‘/’,
}

function products_parse_request( $query ) {

if ( ! $query->is_main_query() || 6 != count( $query->query ) || ! isset( $query->query[‘page’] ) ) {
return;
}

if ( ! empty( $query->query[‘name’] ) ) {
$query->set( ‘post_type’, array( ‘post’, self::$post_type, ‘page’ ) );
}

}

/**
* Register post type
*/
private static function register_post_types() {

$post_types = array();

$labels = array(
‘name’ => __(‘Products’, THEME_TEXT_DOMAIN),
‘singular_name’ => __(‘Product’, THEME_TEXT_DOMAIN),
‘menu_name’ => __(‘Products’, THEME_TEXT_DOMAIN),
‘name_admin_bar’ => __(‘Product’, THEME_TEXT_DOMAIN),
‘add_new’ => __(‘Add New’, THEME_TEXT_DOMAIN),
‘add_new_item’ => __(‘Add New Product’, THEME_TEXT_DOMAIN),
‘new_item’ => __(‘New Product’, THEME_TEXT_DOMAIN),
‘edit_item’ => __(‘Edit Product’, THEME_TEXT_DOMAIN),
‘view_item’ => __(‘View Product’, THEME_TEXT_DOMAIN),
‘all_items’ => __(‘All Products’, THEME_TEXT_DOMAIN),
‘search_items’ => __(‘Search Products’, THEME_TEXT_DOMAIN),
‘parent_item_colon’ => __(‘Parent Products:’, THEME_TEXT_DOMAIN),
‘not_found’ => __(‘No Products found.’, THEME_TEXT_DOMAIN),
‘not_found_in_trash’ => __(‘No Products found in Trash.’, THEME_TEXT_DOMAIN)
);

$post_types[] =
array(
‘post_type’ => ‘products’,
‘args’ => array(
‘labels’ => $labels,
‘public’ => true,
‘publicly_queryable’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘query_var’ => true,
// ‘taxonomies’ => array(‘_product_types’, ‘_product_countries’),
‘rewrite’ => array( ‘slug’ => ‘products/%product_holiday%/%_product_countries%/%_product_types%’ ),
‘capability_type’ => ‘post’,
‘has_archive’ => false,
‘hierarchical’ => true,
‘menu_icon’ => ‘dashicons-tag’,
‘menu_position’ => 41,
‘supports’ => array(‘title’, ‘thumbnail’, ‘page-attributes’)
)
);

foreach($post_types as $post_type) {
register_post_type($post_type[‘post_type’], $post_type[‘args’]);
}
}

This code does work for the URL structure /products/holiday/country/product-type however I need to remove /products … If I remove it from the post type it works but then the rest of the pages on WordPress breaks so I am guessing there is something else I need to add here?

Any help would be greatly appreciated.

Thanks

Read more here:: Remove CPT Slug with 2 taxonomies and 1 post meta

Leave a Reply

Your email address will not be published. Required fields are marked *