Я хочу использовать прикрепленное имя сообщения проекта в ссылке:
Текущая структура ссылки:
domain / {item-post-type-slug} / {item-name}
Я хочу изменить на :
domain / {project->post_name} / {item-name
С текущим кодом должно быть сброшено переписать правила для каждого действия сохранения/удаления в типах сообщений проектов?
Как перенаправить старую структуру ссылок на новую?
Код: Выделить всё
add_action( 'init', 'add_rewrite_rules' );
function add_rewrite_rules() {
$arg = array(
'post_type' => 'projects',
'no_conflict' => '1',
'posts_per_page' => '-1'
);
$projects= new WP_Query($arg);
while($projects->have_posts() ) : $projects->the_post();
global $post;
add_rewrite_rule( $post->post_name.'/([^/]+)/?$', 'index.php?item=$matches[1]', 'top');
endwhile;
}
add_filter( 'post_type_link', 'custom_permalinks', 10, 2 );
function custom_permalinks( $permalink, $post ) {
if ( $post->post_type !== 'item' )
return $permalink;
$project_id = get_field('project', $post->ID);
$project = get_post($project_id);
$new_permalink = str_replace("item", $project->post_name, $permalink);
return $new_permalink;
}
Код: Выделить всё
function flush_project_links( $post_id) {
if ( get_post_type( $post_id ) != 'projects' )
return;
flush_rewrite_rules();
}
add_action('delete_post', 'flush_project_links', 99, 2);
add_action('save_post', 'flush_project_links', 99, 2);
ОБНОВЛЕНО:исправлена перезапись сброса:
Код: Выделить всё
function flush_project_links( $post_id) {
if ( get_post_type( $post_id ) != 'projects' )
return;
add_rewrite_rules()
flush_rewrite_rules();
}
add_action('delete_post', 'flush_project_links', 99, 2);
add_action('save_post', 'flush_project_links', 99, 2);
Подробнее здесь: https://stackoverflow.com/questions/347 ... -post-meta