php - Adding custom field to permalink structure -
i have wordpress site uses custom post type 'external-projects'. within custom post type there custom field 'director'. trying rewrite permalinks include directors name. have working on external-projects messes rest of sites urls. 404s etc. below code. great.
add_action('init', 'tdd_add_rewrite_rules', 10, 0); function tdd_add_rewrite_rules(){ // register custom rewrite rules global $wp_rewrite; $wp_rewrite->add_rewrite_tag('%external-projects%', '([^/]+)', 'external-projects='); $wp_rewrite->add_rewrite_tag('%director%', '([^/]+)', 'director='); $wp_rewrite->add_permastruct('external-projects', '/%director%/%external-projects%', false); } add_filter('post_type_link', 'tdd_permalinks', 10, 3); function tdd_permalinks($permalink, $post, $leavename){ $no_data = 'no-director'; $post_id = $post->id; if($post->post_type != 'external-projects' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) return $permalink; //$var1 = get_post_meta($post_id, 'director', true); $var1 = 'test_director';// i'll later. $var1 = sanitize_title($var1); if(!$var1) { $var1 = $no_data; } $permalink = str_replace('%director%', $var1, $permalink); return $permalink; }
Comments
Post a Comment