基于其他自定义帖子类型标题的自定义重写规则

时间:2021-10-13 作者:Mastafu

我正在尝试根据另一个自定义帖子的名称更改一个自定义帖子类型的url。为了实现这一点。

第一个自定义post/测试1/aaa

重写后的其他自定义帖子

/测试1/aaa/123

/测试1/aaa/456

/测试1/aaa/785

等等

这些自定义贴子仅与ACF对象字段连接。

到目前为止,我所得到的几乎是有效的

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 );
这是为了将公寓名称改为id

//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 );
因此,使用该代码,在保存帖子的同时,另一个海关帖子的url会被很好地更改。但当我打开该url时,它会重定向到第一个自定义帖子。

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\'
        )

1 个回复
SO网友:Mastafu

因此,经过两天的搜索,我找到了一个解决方案。

Permalink manager lite+此功能,一切正常。

//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);