在搜索了很多之后,我终于找到了这个解决方案。
我的自定义帖子类型为“facility“和custom\\u user\\u meta=”organization_name“。
首先转到http://localhost/wp-admin/options-permalink.php
并在文本框中选择“自定义结构”/%organization_name%/%postname%/
然后输入下面给定的代码functions.php
add_action(\'init\', \'tdd_add_rewrite_rules2\');
function tdd_add_rewrite_rules2(){
// Register custom rewrite rules
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag(\'%facility%\', \'([^/]+)\', \'facility=\');
$wp_rewrite->add_rewrite_tag(\'%organization_name%\', \'([^/]+)\', \'\');
$wp_rewrite->add_permastruct(\'facility\', \'/%organization_name%/facility/%facility%\', false);
}
add_filter(\'post_type_link\', \'tdd_permalinks2\', 10, 3);
function tdd_permalinks2($permalink, $post, $leavename){
$no_data = get_the_author_meta(\'ID\');;
$post_id = $post->ID;
if($post->post_type != \'facility\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\'))) return $permalink;
// $var1 = get_post_meta($post_id, \'posts_solicitorspeciality\', true);
$var1 = get_the_author_meta(\'organization_name\');
$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
$permalink = str_replace(\'%organization_name%\', $var1, $permalink);
return $permalink;
}