admin管理员组文章数量:1279050
I am trying to change url of one custom posts type based on name of the other one. To achieve this.
first custom post /test1/aaa
the other custom posts aftere rewrite
/test1/aaa/123
/test1/aaa/456
/test1/aaa/785
and so on.
These custom posts are connected with ACF Object field only.
So what I got so far and it almost works is
function apartments_list() {
$labels = array(
'name' => _x( 'Mieszkanie', 'post type general name' ),
'singular_name' => _x( 'Mieszkania', 'post type singular name' ),
'add_new' => _x( 'Dodaj', 'kategoria' ),
'add_new_item' => __( 'Dodaj' ),
'edit_item' => __( 'Edytuj' ),
'new_item' => __( 'Nowy element' ),
'all_items' => __( 'Wszystkie elementy' ),
'view_item' => __( 'Zobacz element' ),
'search_items' => __( 'Szukaj' ),
'not_found' => __( 'Brak elementów' ),
'not_found_in_trash' => __( 'Elementy w koszu' ),
'parent_item_colon' => '',
'menu_name' => 'Mieszkania'
);
$args = array(
'labels' => $labels,
'description' => 'Mieszkania',
'public' => true,
'menu_position' => 20,
'show_ui' => true,
'show_in_rest' => true,
'show_in_admin_bar'=> true,
'hierarchical' => false,
//'has_archive' => true,
'menu_icon' => 'dashicons-editor-kitchensink',
'supports' => array('title', 'page-attributes'),
'rewrite' => array(
'with_front' => false,
'slug' => '/inwestycja/%mieszkanie_slug%'
//'slug' => '/inwestycja/lofthaus-zablocie-test'
)
);
register_post_type( 'mieszkanie', $args );
}
add_action( 'init', 'apartments_list' );
//Rewrite rule
function change_apartment_url( $url, $post ) {
if ( 'mieszkanie' == get_post_type( $post ) ) {
$inwestycja = get_field('inwestycja');
$inwestycjaObj = get_post($inwestycja);
//$inwest_slug = 'lofthaus-zablocie-test';
$inwest_slug = $inwestycjaObj->post_name;
return str_replace('%mieszkanie_slug%', $inwest_slug, $url);
}
return $url;
}
add_filter( 'post_type_link', 'change_apartment_url', 10, 2 );
and this for changing apartments name to id only
//Change apartment slug to post ID
function slug_save_post_callback( $post_ID, $post, $update ) {
// allow 'publish', 'draft', 'future'
if ($post->post_type == 'mieszkanie') {
// only change slug when the post is created (both dates are equal)
if ($post->post_date_gmt != $post->post_modified_gmt)
return;
$new_slug = sanitize_title( $post->id, $post_ID );
//$new_slug .= '-' . $subtitle;
if ($new_slug == $post->id)
return; // already set
// unhook this function to prevent infinite looping
remove_action( 'save_post', 'slug_save_post_callback', 10, 3 );
// update the post slug (WP handles unique post slug)
wp_update_post( array(
'ID' => $post_ID,
'post_name' => $new_slug
));
// re-hook this function
add_action( 'save_post', 'slug_save_post_callback', 10, 3 );
}
}
add_action( 'save_post', 'slug_save_post_callback', 10, 3 );
So with that code the other customs post's url is being changed fine, while saving the post. But then when I open that url, it redirects to the first custom post.
Only when I change slug to fixed name and save perma links to clear rules, it works. But I need it to be dynamic. Any idea ?
'rewrite' => array(
'with_front' => false,
//'slug' => '/investment/%mieszkanie_slug%'
'slug' => '/investment/lofthouse'
)
I am trying to change url of one custom posts type based on name of the other one. To achieve this.
first custom post /test1/aaa
the other custom posts aftere rewrite
/test1/aaa/123
/test1/aaa/456
/test1/aaa/785
and so on.
These custom posts are connected with ACF Object field only.
So what I got so far and it almost works is
function apartments_list() {
$labels = array(
'name' => _x( 'Mieszkanie', 'post type general name' ),
'singular_name' => _x( 'Mieszkania', 'post type singular name' ),
'add_new' => _x( 'Dodaj', 'kategoria' ),
'add_new_item' => __( 'Dodaj' ),
'edit_item' => __( 'Edytuj' ),
'new_item' => __( 'Nowy element' ),
'all_items' => __( 'Wszystkie elementy' ),
'view_item' => __( 'Zobacz element' ),
'search_items' => __( 'Szukaj' ),
'not_found' => __( 'Brak elementów' ),
'not_found_in_trash' => __( 'Elementy w koszu' ),
'parent_item_colon' => '',
'menu_name' => 'Mieszkania'
);
$args = array(
'labels' => $labels,
'description' => 'Mieszkania',
'public' => true,
'menu_position' => 20,
'show_ui' => true,
'show_in_rest' => true,
'show_in_admin_bar'=> true,
'hierarchical' => false,
//'has_archive' => true,
'menu_icon' => 'dashicons-editor-kitchensink',
'supports' => array('title', 'page-attributes'),
'rewrite' => array(
'with_front' => false,
'slug' => '/inwestycja/%mieszkanie_slug%'
//'slug' => '/inwestycja/lofthaus-zablocie-test'
)
);
register_post_type( 'mieszkanie', $args );
}
add_action( 'init', 'apartments_list' );
//Rewrite rule
function change_apartment_url( $url, $post ) {
if ( 'mieszkanie' == get_post_type( $post ) ) {
$inwestycja = get_field('inwestycja');
$inwestycjaObj = get_post($inwestycja);
//$inwest_slug = 'lofthaus-zablocie-test';
$inwest_slug = $inwestycjaObj->post_name;
return str_replace('%mieszkanie_slug%', $inwest_slug, $url);
}
return $url;
}
add_filter( 'post_type_link', 'change_apartment_url', 10, 2 );
and this for changing apartments name to id only
//Change apartment slug to post ID
function slug_save_post_callback( $post_ID, $post, $update ) {
// allow 'publish', 'draft', 'future'
if ($post->post_type == 'mieszkanie') {
// only change slug when the post is created (both dates are equal)
if ($post->post_date_gmt != $post->post_modified_gmt)
return;
$new_slug = sanitize_title( $post->id, $post_ID );
//$new_slug .= '-' . $subtitle;
if ($new_slug == $post->id)
return; // already set
// unhook this function to prevent infinite looping
remove_action( 'save_post', 'slug_save_post_callback', 10, 3 );
// update the post slug (WP handles unique post slug)
wp_update_post( array(
'ID' => $post_ID,
'post_name' => $new_slug
));
// re-hook this function
add_action( 'save_post', 'slug_save_post_callback', 10, 3 );
}
}
add_action( 'save_post', 'slug_save_post_callback', 10, 3 );
So with that code the other customs post's url is being changed fine, while saving the post. But then when I open that url, it redirects to the first custom post.
Only when I change slug to fixed name and save perma links to clear rules, it works. But I need it to be dynamic. Any idea ?
'rewrite' => array(
'with_front' => false,
//'slug' => '/investment/%mieszkanie_slug%'
'slug' => '/investment/lofthouse'
)
Share
Improve this question
asked Oct 13, 2021 at 8:28
MastafuMastafu
457 bronze badges
1 Answer
Reset to default 0So after 2 days of searching I found a solution.
Permalink manager lite + this function and all works fine.
//Rewrite rule
function pm_extra_permastructure_tags($default_uri, $native_slug, $post, $slug, $native_uri) {
// Do not affect native URIs
if($native_uri == true || empty($post->post_type) || $post->post_type !== 'mieszkanie') { return $default_uri; }
$inwestycja = get_field('inwestycja', $post->ID);
if( !empty($inwestycja) ) {
$inwestycjaObj = get_post($inwestycja);
$default_uri = str_replace('%mieszkanie_slug%', $inwestycjaObj->post_name, $default_uri);
}
return $default_uri;
}
add_filter('permalink_manager_filter_default_post_uri', 'pm_extra_permastructure_tags', 3, 5);
本文标签: url rewritingCustom rewrite rule based on other custom post type title
版权声明:本文标题:url rewriting - Custom rewrite rule based on other custom post type title 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741267456a2368747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论