Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created hierarchy based custom permalinks with help of this link.

But now it is causing 404 error on another custom taxonomy used in site. Although I want custom permalinks on this taxonomy also, but if I don't use any rewrite argument and leave it on its default permalink it is still not working. My code for second taxonomy is this:

register_post_type( 'gallery',
    array(
        'public' => true,
        'menu_position' => 15,
        'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
        'taxonomies' => array( '' ),
        'menu_icon' => get_template_directory_uri().'/img/icon_gallery.png',
    'register_meta_box_cb' => 'add_gallery_metaboxes'
    )
);
register_taxonomy('gallery_category','gallery',array(
    'hierarchical' => true,
    'labels' => $labels
  ));

For more assistance am also mentioning my first custom taxonomy code:

register_post_type( 'article',
    array(
        'public' => true,
    'hierarchical' => true,
        'menu_position' => 15,
        'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
        'taxonomies' => array( '' ),
        'menu_icon' => get_template_directory_uri().'/img/icon_article.png',
    'query_var' => true,
    'rewrite' => array(
            'slug' => '%article_category%/articles',
            'with_front' => true
        ),
    'has_archive' => '%article_category%',
    'register_meta_box_cb' => 'add_article_metaboxes'
    )
);

register_taxonomy('article_category','article',array(
  'hierarchical' => true,
  'labels' => $labels,
  'query_var' => true,
  'rewrite' => array('slug' => '', 'hierarchical' => true),
));

Function for custom permalink:

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

if ($cats = get_the_terms($post->ID, 'article_category'))
{
    $link = str_replace('%article_category%', get_taxonomy_parents(array_pop($cats)->term_id, 'article_category', false, '/', true), $link); // see custom function defined below
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
share|improve this question
    
Have you flushed the rewrite rules? –  s_ha_dum May 28 '13 at 14:30
    
Yes I did it by reloading the settings->permalinks page. –  Noman May 28 '13 at 14:36
    
If I remove the rewrite attribute from first custom taxonomy then both work fine but custom permalinks stop. –  Noman May 28 '13 at 14:37
    
You mention two custom taxonomies. Post the registration code for both. Post your code that you based off of the other question you linked to. –  s_ha_dum May 28 '13 at 14:38
    
Okay added the code for first custom taxonomy in question. –  Noman May 28 '13 at 14:45

1 Answer 1

After many efforts I have solved it. But not completely what I want, had to compromise on one thing and passed some separators in between Url. Example: http://sitename.com/separator1/parentatx/childtax/separator2/postname/

And in order to avoid 404 errors in case of pagination and another custom taxonomy. I created some rewrite rules. Here is my code:

First Post type and Custom Taxonomy Registration Code:

register_post_type( 'article',
    array(
        'public' => true,
        'hierarchical' => true,
        'menu_position' => 15,
        'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
        'taxonomies' => array( '' ),
        'menu_icon' => get_template_directory_uri().'/img/icon_article.png',
        'query_var' => true                              
        'rewrite' => array(
            'slug' => 'articles/%article_category%/topic/',
            'with_front' => true
        ),
    'has_archive' => '/all-articles/',
       'register_meta_box_cb' => 'add_article_metaboxes'
      )
   );

register_taxonomy('article_category','article',array(
                'hierarchical' => true,
                'labels' => $labels,
                'query_var' => true,
                'rewrite' => array('slug' => '/articles/', 'hierarchical' => true),
                ));

In first custom taxonomy, I took 'articles' and 'topic' as separator. You can take any according to your scenario.

Second Post type and Custom Taxonomy registration code:

register_post_type( 'gallery',
    array(
    'public' => true,
    'menu_position' => 15,
    'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
    'taxonomies' => array( '' ),
    'menu_icon' => get_template_directory_uri().'/img/icon_gallery.png',
    'query_var' => true,
    'rewrite' => array(
            'slug' => 'galleries/%gallery_category%/images/',
            'with_front' => true
            ),
     'has_archive' => '/all-galleries/',
     'register_meta_box_cb' => 'add_gallery_metaboxes'
       )
   );

In second custom taxonomy, I took 'galleries' and 'images' as separator. You can take any according to your scenario.

And Now Function for rewrite rules:

   add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
   function mmp_rewrite_rules($rules) {
    $newRules  = array();
    //Rules for First Taxonomy
    $newRules['articles/(.+)/(.+)/(topic)/(.+)/?$'] = 'index.php?article=$matches[4]'; 
$newRules['articles/(.+)/(topic)/(.+)/?$'] = 'index.php?article_category=$matches[1]&article=$matches[3]';
$newRules['articles/(.+)/(.+)/?([0-9]{1,})/?$'] = 'index.php?article_category=$matches[1]&page=$matches[3]';
$newRules['articles/(.+)/(.+)/(page)/?([0-9]{1,})/?$'] = 'index.php?article_category=$matches[1]&page=$matches[4]';
$newRules['articles/(.+)/?$'] = 'index.php?article_category=$matches[1]';
//Rules for Second Taxonomy
$newRules['galleries/(.+)/(.+)/(images)/(.+)/?$'] = 'index.php?gallery=$matches[4]'; 
$newRules['galleries/(.+)/(images)/(.+)/?$'] = 'index.php?gallery_category=$matches[1]&gallery=$matches[3]';
$newRules['galleries/(.+)/(.+)/?([0-9]{1,})/?$'] = 'index.php?gallery_category=$matches[1]&page=$matches[3]';
$newRules['galleries/(.+)/(.+)/(page)/?([0-9]{1,})/?$'] = 'index.php?gallery_category=$matches[1]&page=$matches[4]';
$newRules['galleries/(.+)/?$'] = 'index.php?gallery_category=$matches[1]'; 

return array_merge($newRules, $rules);

}

Now functions to change the custom post type URL according to custom taxonomy hierarchy.

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

if ($post->post_type == 'article')  
{
    if ($cats = get_the_terms($post->ID, 'article_category'))
    {
        $link = str_replace('%article_category%',   get_taxonomy_parents(array_pop($cats)->term_id, 'article_category', false, '/', true), $link); // see custom function defined below
    }
    return $link;
}
elseif($post->post_type == 'gallery')
{
    if ($cats = get_the_terms($post->ID, 'gallery_category'))
    {
        $link = str_replace('%gallery_category%', get_taxonomy_parents(array_pop($cats)->term_id, 'gallery_category', false, '/', true), $link); // see custom function defined below
    }
    return $link;
}
 }
 add_filter('post_type_link', 'filter_post_type_link', 10, 2);
// my own function to do what get_category_parents does for other taxonomies
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;    
 }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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