Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Post Type, Taxonomy and Permalinks Re-write WordPress

  • REFUNDED

I'm having a few issues setting up the correct permalink structure for a Custom Post Type I've created for a new project.

Essentially what I need to do is written in the following web post but I can't get it to function right. I keep getting a double forward slash before the post name. Unfortunately I don't know enough php to get it sorted.
http://wordpress.stackexchange.com/questions/39500/how-to-create-a-permalink-structure-with-custom-taxonomies-and-custom-post-types/39862#39862

What I need to start with is Custom Post Type called New Cars, with an archive for all posts that can be accessed with this link:
http://www.mydomain.com/new-cars

Then I need to have a taxonomy of Make & Model but with a rewrite function to name the archive like so:
http://www.mydomain.com/new-cars/manufacturers
This archive would list all the manufacturer names added to the make & model taxonomy.

There after I need a further taxonomy child term archive for each relative taxonomy term. e.g:
http://www.mydomain.com/new-cars/manufacturers/audi
Which would list every Audi new car post.

Then one final archive that lists all the posts within the taxonomy child term e.g
http://www.my domain.com/new-cars/manufacturers/audi/a3
Obviously this would list all the Audi A3 car posts.

Finally the end post needs to have all the added taxonomy parent and child terms added to it. e.g.
http://www.mydomain.com/new-cars/audi/a3/1-4-diesel/

I'm fine with setting up all the required archive templates, I'm just having issues setting up the post type and taxonomy permalink structure via the functions.php file.

I have the existing code I've been using should anyone need it.

Many thanks,

Adam.

Answers (5)

2012-09-03

Michael Caputo answers:

please post your existing code


flint_and_tinder comments:

Of course, here is where I'm at currently:

add_action( 'init', 'register_taxonomy_make_model' );

function register_taxonomy_make_model() {

$labels = array(
'name' => _x( 'Make & Models', 'make_model' ),
'singular_name' => _x( 'Make & Model', 'make_model' ),
'search_items' => _x( 'Search Make & Model', 'make_model' ),
'popular_items' => _x( 'Popular Make & Model', 'make_model' ),
'all_items' => _x( 'All Make & Model', 'make_model' ),
'parent_item' => _x( 'Parent Make & Model', 'make_model' ),
'parent_item_colon' => _x( 'Parent Make & Model:', 'make_model' ),
'edit_item' => _x( 'Edit Make & Model', 'make_model' ),
'update_item' => _x( 'Update Make & Model', 'make_model' ),
'add_new_item' => _x( 'Add New Make & Model', 'make_model' ),
'new_item_name' => _x( 'New Make & Model', 'make_model' ),
'separate_items_with_commas' => _x( 'Separate make & model with commas', 'make_model' ),
'add_or_remove_items' => _x( 'Add or remove make & model', 'make_model' ),
'choose_from_most_used' => _x( 'Choose from the most used make & model', 'make_model' ),
'menu_name' => _x( 'Make & Model', 'make_model' ),
);

$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'args' => array( 'orderby' => 'term_order' ),
'show_ui' => true,
'hierarchical' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'new-cars/manufacturers',
'hierarchical' => true,
'query_var' => true
)

);

register_taxonomy( 'make_model', array('new_cars'), $args );
}

add_action( 'init', 'register_cpt_new_cars' );

function register_cpt_new_cars() {

$labels = array(
'name' => _x( 'New Cars', 'new_cars' ),
'singular_name' => _x( 'New Car', 'new_cars' ),
'add_new' => _x( 'Add New', 'new_cars' ),
'add_new_item' => _x( 'Add New New Car', 'new_cars' ),
'edit_item' => _x( 'Edit New Cars', 'new_cars' ),
'new_item' => _x( 'New New Car', 'new_cars' ),
'view_item' => _x( 'View New Cars', 'new_cars' ),
'search_items' => _x( 'Search New Cars', 'new_cars' ),
'not_found' => _x( 'No new cars found', 'new_cars' ),
'not_found_in_trash' => _x( 'No new cars found in Trash', 'new_cars' ),
'parent_item_colon' => _x( 'Parent New Car:', 'new_cars' ),
'menu_name' => _x( 'New Cars', 'new_cars' ),
);

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

'supports' => array( 'title', 'editor', 'thumbnail' ),

'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,

'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array(
'slug' => 'new-cars/manufacturers/%taxonomy_name%',
'hierarchical' => true,
'query_var' => true
),
'capability_type' => 'post'
);

register_post_type( 'new_cars', $args );
}


add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules['new-cars/(.+)/(.+)/?$'] = 'index.php?custom_post_type_name=$matches[2]';
$newRules['new-cars/(.+)/?$'] = 'index.php?taxonomy_name=$matches[1]';

return array_merge($newRules, $rules);
}

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

if ($cats = get_the_terms($post->ID, 'make_model'))
{
$link = str_replace('%taxonomy_name%', get_taxonomy_parents(array_pop($cats)->term_id, 'make_model', false, '/', true), $link); // see custom function defined below
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) {
return $parent;
}

if ($nicename)
$name = $parent -> slug;
else
$name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {
$visited[] = $parent -> parent;
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

}

if ($link) {
// nothing, can't get this working :(
} else
$chain .= $name . $separator;
return $chain;
}


Michael Caputo comments:

Here's code for your post type "new-cars" & taxonomy "manufaturers" - keep in mind you need to create an "archive-new-cars.php" to display your http://www.mydomain.com/new-cars/ URL
add_action( 'init', 'register_cpt_new-cars' );

function register_cpt_new-cars() {

$labels = array(
'name' => _x( 'New Cars', 'new-cars' ),
'singular_name' => _x( 'Car', 'new-cars' ),
'add_new' => _x( 'Add New', 'new-cars' ),
'add_new_item' => _x( 'Add New Car', 'new-cars' ),
'edit_item' => _x( 'Edit Car', 'new-cars' ),
'new_item' => _x( 'New Car', 'new-cars' ),
'view_item' => _x( 'View Car', 'new-cars' ),
'search_items' => _x( 'Search New Cars', 'new-cars' ),
'not_found' => _x( 'No new cars found', 'new-cars' ),
'not_found_in_trash' => _x( 'No new cars found in Trash', 'new-cars' ),
'parent_item_colon' => _x( 'Parent Car:', 'new-cars' ),
'menu_name' => _x( 'New Cars', 'new-cars' ),
);

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

'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'manufacturers' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,

'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => 'new-cars',
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);

register_post_type( 'new-cars', $args );
}

add_action( 'init', 'register_taxonomy_manufacturers' );

function register_taxonomy_manufacturers() {

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

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

'rewrite' => true,
'query_var' => true
);

register_taxonomy( 'manufacturers', array('new-cars'), $args );
}


flint_and_tinder comments:

When C+P'd into my functions.php, I'm getting a syntax error starting on this line:

function register_cpt_new-cars() {


flint_and_tinder comments:

Hi Michael,

I changed:
function register_cpt_new-cars () {
to
function register_cpt_new_cars () {

which resolved the syntax error. However when I add a new 'New Car' post the 'manufacturer' taxonomy parent term and child term is not being added to the final post permalink.

For example I am getting:

www.mydomain.com/new-cars/1-4-diesel

but what I need is:

www.mydomain.com/new-cars/manufacturers/audi/a3/1-4-diesel

Please see screenshot for example.