I have a custom post type "goal" setup. Regular posts are assigned to each goal, resulting in permalinks like http://example.com/credit/fix-my-credit/learn-how (format is %category%/%goal%/%postname%
) where:
- category (slug) = 'credit'
- goal (slug) = 'fix-my-credit'
- post (slug) = 'learn-how'
The permalinks work for the most part. However, in the admin side, for example when editing a post the permalink is correct at the top of the edit page, but the "View Post" link in the top admin bar looks like http://example.com/credit/%goal%/learn-how. I see the same thing in links under recent comments on the admin dashboard. Setup:
Settings >> Permalink >> Custom Structure: /%category%/%goal%/%postname%/
Custom Post:
$labels = array(
'name' => _x( 'Goals', 'Post Type General Name', 'bonestheme' ),
'singular_name' => _x( 'Goals', 'Post Type Singular Name', 'bonestheme' ),
'menu_name' => __( 'Goals', 'bonestheme' ),
'name_admin_bar' => __( 'Goals', 'bonestheme' ),
'archives' => __( 'Goal Archives', 'bonestheme' ),
'parent_item_colon' => __( 'Parent Goal:', 'bonestheme' ),
'all_items' => __( 'All Goals', 'bonestheme' ),
'add_new_item' => __( 'Add New Goal', 'bonestheme' ),
'add_new' => __( 'Add New Goal', 'bonestheme' ),
'new_item' => __( 'New Goal', 'bonestheme' ),
'edit_item' => __( 'Edit Goal', 'bonestheme' ),
'update_item' => __( 'Update Goal', 'bonestheme' ),
'view_item' => __( 'View Goal', 'bonestheme' ),
'search_items' => __( 'Search Goals', 'bonestheme' ),
'not_found' => __( 'Not found', 'bonestheme' ),
'not_found_in_trash' => __( 'Not found in Trash', 'bonestheme' ),
'featured_image' => __( 'Featured Image', 'bonestheme' ),
'set_featured_image' => __( 'Set featured image', 'bonestheme' ),
'remove_featured_image' => __( 'Remove featured image', 'bonestheme' ),
'use_featured_image' => __( 'Use as featured image', 'bonestheme' ),
'insert_into_item' => __( 'Insert into item', 'bonestheme' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'bonestheme' ),
'items_list' => __( 'Items list', 'bonestheme' ),
'items_list_navigation' => __( 'Items list navigation', 'bonestheme' ),
'filter_items_list' => __( 'Filter items list', 'bonestheme' ),
);
$args = array(
'label' => __( 'Goals', 'bonestheme' ),
'description' => __( 'Post Type Description', 'bonestheme' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'comments' ),
'taxonomies' => array( 'category', 'tag' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'query_var' => true,
'rewrite' => array( 'slug' => 'goal', 'with_front' => false ),
);
Rewrite:
function custom_rewrite_basic() {
add_rewrite_rule(
'^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?',
'index.php?category_name=$matches[1]&goal=$matches[2]&goal_article=$matches[3]',
'top'
);
}
add_action('init', 'custom_rewrite_basic');
Permalink filter:
function update_article_permalink( $permalink, $post_id ) {
global $post;
if ( get_post_type($post_id) === 'post' ) {
$the_goal = get_article_goal($post_id);
$post = get_post($post_id);
$post->post_name = sanitize_title($post->post_title);
$post_link = $permalink;
if (!empty($the_goal)) {
$goal_slug = $the_goal['slug'];
$post_link = str_replace( '%goal%', $goal_slug, $permalink );
} else {
$slug = $post->post_name;
$post_link = str_replace( '%goal%/', '', $permalink );
}
$permalink = $post_link;
}
if ( get_post_type($post_id) === 'goal' ) {
$categories = get_the_category($post_id);
if (count($categories) > 0){
$first_cat = $categories[0]->slug;
$permalink = preg_replace( '(\/goal\/)', '/'.$first_cat.'/', $permalink );
}
}
return $permalink;
}
add_filter( 'get_sample_permalink', 'update_article_permalink', 10, 2 );
What have I missed? How can I get %goal%
replaced in all permalinks?