How to have post_type_link filter alterations be reflected on admin post edit view?
Question
say this:
// from register_post_type
,'rewrite' => array(
'slug' => 'prodotti/%product_categories%',
'with_front' => false,
'pages' => false
)
then
add_filter('post_type_link', 'lr_fix_permalinks' , 10, 2);
function lr_fix_permalinks($post_link, $post){
if(is_wp_error($post) || $post->post_type !== 'product' || empty($post->post_name)) return $post_link;
$terms = get_the_terms($post->ID, 'product_categories');
if(is_wp_error($terms) || !$terms) return $post_link;
$ordered_terms = array();
list_terms_by_parent(0, $terms, $ordered_terms);
$ordered_terms_length = count($ordered_terms);
if($ordered_terms_length <= 1) return $post_link;
$product_category = $ordered_terms[$ordered_terms_length - 2];
$products_slug = pll__('products');
return home_url(user_trailingslashit("$products_slug/$product_category->slug/$post->post_name"));
}
add_filter('wp_insert_post_data', 'update_post_data' , 10, 2);
function update_post_data($data, $postarr){
if($data['post_type'] !== 'product') return $data;
$data['post_name'] = wp_unique_post_slug(sanitize_title($data['post_title']), $postarr['ID'], $data['post_status'], $data['post_type'], $data['post_parent']);
return $data;
}
It all works perfectly, meaning that in frontend, when asking for a product url, it will be correctly created.
My issue is a minor issue and has to do only with admin product edit page. Please see the attachment:
As you can see, %product_categories% is visualized instead of the real term slug. Therefore, clicking the link will lead to a 404, as well as clicking the preview button from the admin. How to avoid? Any other filter to use maybe?
0
custom-post-types, custom-taxonomy, permalinks
4 years
2019-11-29T07:09:54-05:00
2019-11-29T07:09:54-05:00 0 Answers
90 views
0
Leave an answer