0

I have a custom post type, Project:

/*******************************
Projects Custom Post Type
*******************************/
add_action( 'init', 'register_cpt_project' );

function register_cpt_project() {

$labels = array( 
    'name' => _x( 'Projects', 'project' ),
    'singular_name' => _x( 'Project', 'project' ),
    'add_new' => _x( 'Add New', 'project' ),
    'add_new_item' => _x( 'Add New Project', 'project' ),
    'edit_item' => _x( 'Edit Project', 'project' ),
    'new_item' => _x( 'New Project', 'project' ),
    'view_item' => _x( 'View Project', 'project' ),
    'search_items' => _x( 'Search Projects', 'project' ),
    'not_found' => _x( 'No projects found', 'project' ),
    'not_found_in_trash' => _x( 'No projects found in Trash', 'project' ),
    'parent_item_colon' => _x( 'Parent Project:', 'project' ),
    'menu_name' => _x( 'Projects', 'project' ),
);

$args = array( 
    'labels' => $labels,
    'hierarchical' => true,

    'supports' => array( 'title', 'editor', 'excerpt' ),
    'taxonomies' => array( 'project_categories' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,


    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    //'rewrite' => true,
    'rewrite' => array('slug' => 'our-work/%project_categories%', 'with_front' => false),
    'capability_type' => 'post'
);

    register_post_type( 'project', $args );
}

And a Taxonomy, Project Categories:

/*******************************
Projects Category Taxonomy
 *******************************/
add_action( 'init', 'register_taxonomy_project_categories' );

function register_taxonomy_project_categories() {

$labels = array( 
    'name' => _x( 'Project Categories', 'project_categories' ),
    'singular_name' => _x( 'Project Category', 'project_categories' ),
    'search_items' => _x( 'Search Project Categories', 'project_categories' ),
    'popular_items' => _x( 'Popular Project Categories', 'project_categories' ),
    'all_items' => _x( 'All Project Categories', 'project_categories' ),
    'parent_item' => _x( 'Parent Project Category', 'project_categories' ),
    'parent_item_colon' => _x( 'Parent Project Category:', 'project_categories' ),
    'edit_item' => _x( 'Edit Project Category', 'project_categories' ),
    'update_item' => _x( 'Update Project Category', 'project_categories' ),
    'add_new_item' => _x( 'Add New Project Category', 'project_categories' ),
    'new_item_name' => _x( 'New Project Category', 'project_categories' ),
    'separate_items_with_commas' => _x( 'Separate project categories with commas', 'project_categories' ),
    'add_or_remove_items' => _x( 'Add or remove project categories', 'project_categories' ),
    'choose_from_most_used' => _x( 'Choose from the most used project categories', 'project_categories' ),
    'menu_name' => _x( 'Project Categories', 'project_categories' ),
);

$args = array( 
    'labels' => $labels,
    'public' => true,
    'show_in_nav_menus' => true,
    'show_ui' => true,
    'show_tagcloud' => true,
    'show_admin_column' => false,
    'hierarchical' => true,

    //'rewrite' => true,
    'rewrite' => array('slug' => 'our-work'),
    'query_var' => true
);

    register_taxonomy( 'project_categories', array('project'), $args );
}


add_action( 'init', 'register_cpt_leadership' );

In my Taxonomy archive template, I build out the URL to include the taxonomy term. So if "Project A" is in the "Awesome Stuff" category, the URL displays as:

[my domain]/our-work/awesome_stuff/project-a

All of this works perfectly on the front end. What doesn't work is when clicking buttons from the Admin side to view the project. If I'm editing "Project A", and click the View Project button under the permalink section, or the Preview Changes button on the right.

View Project Button

It returns a 400 Bad Request page, and sends the browser to:

[my domain]/our-work/%project_categories%/project-a

How can I make the preview/view buttons in the admin go to:

[my domain]/our-work/project_categories/project-a

That URL works, and since the preview links aren't tied to a specific Taxonomy, this would be the preferred solution.

EDIT: I'm unsure if I'm explaining this right. I do not need to alter the way permalinks are being handled across the site. All I need is a way to hook into any link in the admin section (the button pictured above, the preview button, and the View links on the list page). I need to just set the %project_categories% variable to "project_categories" if the link is generated via the admin.

6
  • Go to Settings->Permalink page and hit on save changes button. It'll flash the rewrite URL and permalink for custom post type will work Sep 5, 2014 at 13:58
  • I tried that again (I always do that after making changes to the rewrite rules on anything) and it hasn't made any change. It still returns %project_categories% as the taxonomy in the URL.
    – Joe
    Sep 5, 2014 at 14:01
  • Ahh...%project_categories% will not work in permalink. You'll need to write a custom code for rewrite URL of custom permalink Sep 5, 2014 at 14:36
  • I know. That's what I'm asking. How do I do that. I need the wp-admin's View and Preview buttons to display a URL with the percent symbols stripped out.
    – Joe
    Sep 5, 2014 at 14:43
  • if you use this permalink /%category%/%postname%/ structure then what is happening? are you tried it? Also you will use this 'rewrite' => array('slug' => 'our-work', 'with_front' => false), Sep 5, 2014 at 14:49

1 Answer 1

1

I found a solution here:

Custom post types, taxonomies, and permalinks

TheDeadMedic's solution worked like a charm.

function filter_post_type_link($link, $post)
{
if ($post->post_type != 'project')
    return $link;

if ($cats = get_the_terms($post->ID, 'project_categories'))
    $link = str_replace('%project_categories%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

This checks to see what Project Categories are associated with the project, and uses the first one to build out the permalink for the Project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.