我正在运行一个网站,允许用户创建一个配置文件,这是一种自定义的帖子类型,通过ACF前端表单提交/编辑。除了用户都使用相同的“标题”(经过消毒并用作永久链接)外,所有操作都按预期进行。
我希望永久链接具有以下“结构”:“post type/city/{post title}-{post id}。我的想法是添加一个帖子id,这样每个链接都是唯一的,但我现在发现情况并非如此。
如果我有两个配置文件:www.domain。com/profile/city/i-am-cool-123www。领域com/profile/city/i-am-cool-456
然后是www.domain。com/profile/city/i-am-cool-456重定向到www.domain。com/profile/city/i-am-cool-123。
我知道你不能有2个相同的永久链接,但我可能误解了永久链接是如何“注册”的。
下面是我的代码。
首先,我为新的变量添加了必要的查询变量,并添加了自定义重写标记。
function sd_custom_rewrite_tag() {
    add_rewrite_tag( \'%city%\', \'([^&]+)\', \'city=\' );
    add_rewrite_tag( \'%postname%\', \'([^&]+)\', \'name=\' );
}
add_action( \'init\', \'sd_custom_rewrite_tag\', 10, 0 );
function sd_add_query_vars( $vars ) {
    $vars[] = "city";
    $vars[] = "postname";
    return $vars;
}
add_filter( \'query_vars\', \'sd_add_query_vars\' );
 为了获得我想要的永久链接,我有以下代码。
function sd_new_profile_permalink( $permalink, $post, $leavename = false ) {
    if ( strpos( $permalink, \'%city%\' ) === FALSE ) {
        return $permalink;
    }
    // Get post
    if ( ! $post ) {
        return $permalink;
    }
    // Get custom info
    $city_info  = get_field( \'sd_city_selector\', $post->ID );
    $post_slug  = $post->post_name;
    if ( ! is_wp_error( $city_info ) && ! empty( $city_info ) ) {
        $city_replace  = str_replace( \'\\\'\', \'\', $city_info[ \'cityName\' ] );
        $city_replace  = str_replace( \' \', \'-\', $city_replace );
        $city_slug     = strtolower( $city_replace );
        $new_permalink = str_replace( array( \'%city%\', \'%postname%\', \'%post_id%\' ), array( $city_slug, $post_slug, $post->ID ), $permalink );
        return $new_permalink;
    }
    return $permalink;
}
add_filter( \'post_link\', \'sd_new_profile_permalink\', 10, 3 );
add_filter( \'post_type_link\', \'sd_new_profile_permalink\', 10, 3 );
 到目前为止,没有什么奇怪的事情发生,这一切都在做它应该做的事情,但现在我们开始着手解决这个问题(我想)。
提交帖子后,我通过WPDB操作更新slug,如下所示。
function set_profile_title_from_headline( $post_id ) {
    if ( empty( $_POST[ \'acf\' ] ) ) {
        return;
    }
    if ( ! empty( $_POST[ \'acf\' ][ \'field_57e3ed6c92ea0\' ] ) ) {
        $entered_title = $_POST[ \'acf\' ][ \'field_57e3ed6c92ea0\' ];
        $cleaned_title = preg_replace( \'/[^A-Za-z0-9\\-\\s]/\', \'\', $entered_title ); 
        $post_name     = sanitize_title( $cleaned_title );
        update_field( \'sd_ad_title\', $cleaned_title, $post_id ); 
        global $wpdb;
        $wpdb->update(
            $wpdb->posts,
            array(
                \'post_title\'  => $cleaned_title,
                \'post_name\'   => $post_name
            ),
            array(
                \'ID\' => $post_id
            )
        );
        clean_post_cache( $post_id );
    }
}
add_action( \'acf/save_post\', \'set_profile_title_from_headline\', 20 );
 最后我重写了url。
function sd_single_profile_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_permastruct( \'profile\', \'profile/%city%/%postname%-%post_id%/\', false );
    add_rewrite_rule( \'profile\\/([a-z-]+)\\/(.+)-[0-9]+\\/?$\', \'index.php?post_type=profile&p=$matches[2]&city=$matches[1]&name=$matches[2]\', \'top\' );
}
add_action( \'init\', \'sd_single_profile_rewrite\' );
 基本上,我的问题是:有没有办法“做”我想做的事?如果是,如何:)