I have a custom post type on my site called Homes, with a custom taxonomy called homes-category. They are both working correctly, but I am having trouble with the permalinks. I need the permalink to be homes/homes-category/pagename. I wrote a rewrite function, so the permalink is showing up correctly whenever I go to a homes item, but I get a 404 on the page itself. I am not sure what is causing this and have no ideas on how to fix it. The homes items work fine without the custom taxonomy in the permalink, but not with it. Does anyone have any ideas on how to fix this? I’ve been searching for days with no luck.

Here is the code for my custom post type:

register_post_type( 'Homes',
    array(
        'labels' => array(
            'name' => __( 'Homes' ),
            'singular_name' => __( 'Homes Item' ),
            'add_new_item' => __('Add New Homes Item'),
            'edit_item' => __('Edit Homes Item'),
            'new_item' => __('New Homes Item'),
        ),
        'supports' => array('title', 'thumbnail', 'editor'),
        'taxonomies' => array('homes-category'),
        'public' => true,
        'has_archive' => false,
        'show_in_nav_menus'   => TRUE,
        'show_in_menu'        => TRUE,
        'rewrite' => array(
            'slug' => 'homes/%homes-category%', 
            'with_front' => true,
            'hierarchical' => true,
           ),
    )
);

Here is the code for my custom taxonomy

register_taxonomy(
    'homes-category',
    'homes',
    array(
        'hierarchical' => true,
        'label' => __( 'Availability Category' ),
        'rewrite' => array( 
            'slug' => 'homes',
            'with_front' => false,
        ),
    )
);

And here is the rewrite function

function custom_post_link($post_link, $id = 0)
{
    $post = get_post($id);
    if(!is_object($post) || $post->post_type != 'project')
{
     return $post_link;
}
    $client = 'misc';
    if($terms = wp_get_object_terms($post->ID, 'client'))
{
    $client = $terms[0]->slug;
//Replace the query var surrounded by % with the slug of 
//the first taxonomy it belongs to.
    return str_replace('%client%', $client, $post_link);
}
//If all else fails, just return the $post_link.
    return $post_link;
}
add_filter('post_type_link', 'custom_post_link', 1, 3);

Read more here: WordPress Custom Type permalink with custom Taxonomy slug


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , ,

Wordpress related questions and answers: