0
votes

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 != 'homes')
{
     return $post_link;
}
    $homes = 'misc';

    if($terms = wp_get_object_terms($post->ID, 'homes-category'))
{
    $client = $terms[0]->slug;

//Replace the query var surrounded by % with the slug of 
//the first taxonomy it belongs to.
    return str_replace('%homes-category%', $homes, $post_link);
}

//If all else fails, just return the $post_link.
    return $post_link;
}

add_filter('post_type_link', 'custom_post_link', 1, 3);
1

1 Answers

0
votes

You need to add rewrite_tag for, '%homes-category%'

Even you need to add, custom rewrite_rule, to interpret it correctly.

You can use below code,

add_action('init','wdm_register_post_types');

function wdm_register_post_types()
{
    global $wp_rewrite;

    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-cat%', 
            'with_front' => true,
            'hierarchical' => true
           ),
    )
  );

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

    $wp_rewrite->add_rewrite_tag( '%homes-cat%', '[a-zA-Z0-9_]');

    add_rewrite_rule('^homes/([^/]+)/([^/]+)/?$','index.php?homes=$matches[2]','top');
}

function wp_tuts_filter_post_link( $permalink, $post ) {

    if ( false === strpos( $permalink, '%homes-cat%' ) )
        return $permalink;

    $terms =  current(wp_get_post_terms( $post->ID, 'homes-category', array('fields' => 'slugs')));

    $permalink = str_replace( '%homes-cat%', $terms , $permalink );

    return $permalink;
}
add_filter( 'post_link', 'wp_tuts_filter_post_link' , 10, 2 );

add_filter('post_type_link','wp_tuts_filter_post_link' , 10, 2 );

This will interpret,

http://domain.com/homes/2-bhk/home-1/

properly. For more details you can also check out this blog post on Creating Custom WordPress Rewrite Rules for Pretty Permalinks