0

My custom taxonomy and custom post type are the followings -

// Creating a house Custom Post Type
function house_custom_post_type() {
    $labels = array(
        'name'                => __( 'house' ),
        'singular_name'       => __( 'house'),
        'menu_name'           => __( 'house'),
        'parent_item_colon'   => __( 'Parent house'),
        'all_items'           => __( 'All house'),
        'view_item'           => __( 'View house'),
        'add_new_item'        => __( 'Add New house'),
        'add_new'             => __( 'Add New house'),
        'edit_item'           => __( 'Edit house'),
        'update_item'         => __( 'Update house'),
        'search_items'        => __( 'Search house'),
        'not_found'           => __( 'Not Found'),
        'not_found_in_trash'  => __( 'Not found in Trash')
    );
    $args = array(
        'label'               => __( 'house'),
        'description'         => __( 'Best house'),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields'),
        'public'              => true,
        'hierarchical'        => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => true,
        'can_export'          => true,
        'exclude_from_search' => false,
        'yarpp_support'       => true,
        'taxonomies'          => array( 'post_tag' ),
        'rewrite'             => array( 'slug' => '%house_type%', 'with_front' => false ), 
        'publicly_queryable'  => true,
        'capability_type'     => 'page'
    );
    register_post_type( 'house', $args );
}
add_action( 'init', 'house_custom_post_type', 0 );

//create a custom taxonomy name it "house_type" for your posts
function house_create_custom_taxonomy() {
    $labels = array(
        'name'              => _x( 'house_type', 'taxonomy general name' ),
        'singular_name'     => _x( 'house_type', 'taxonomy singular name' ),
        'search_items'      => __( 'Search house_type' ),
        'all_items'         => __( 'All house_type' ),
        'parent_item'       => __( 'Parent house_type' ),
        'parent_item_colon' => __( 'Parent house_type: ' ),
        'edit_item'         => __( 'Edit house_type' ), 
        'update_item'       => __( 'Update house_type' ),
        'add_new_item'      => __( 'Add New house_type' ),
        'new_item_name'     => __( 'New house_type Name' ),
        'menu_name'         => __( 'house_type' ),
    );

    register_taxonomy( 'house_type', array( 'house' ), array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'house' ),
    ) );
}
add_action( 'init', 'house_create_custom_taxonomy', 0 );

function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'house' ) );
    }
    return $query;
}
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function mmp_rewrite_rules( $rules ) {
    $newRules = array();
    $newRules['basename/(.+)/?$']                = 'index.php?house_type=$matches[1]'; 
    $newRules['basename/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?house=$matches[4]'; // my custom structure will always have the post name as the 5th uri segment
    return array_merge( $newRules, $rules );
}
add_filter( 'rewrite_rules_array', 'mmp_rewrite_rules' );

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

    if ( $cats = get_the_terms( $post->ID, 'house_type' ) ) {
        $link = str_replace( '%house_type%', get_taxonomy_parents( array_pop( $cats )->term_id, 'house_type', 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 ) {

    } else {
        $chain .= $name . $separator;
    }
    return $chain;
}

I am using this custom function in my localhost all are working good but I have faced two errors in this my permalink structure post type is:

1) http://localhost/project/house/bunglow/5bhk//my-houses/

But near 5BHK create two separators 5bhk//my-houses/ I want to remove one separator, I want only one like this: http://localhost/project/house/bunglow/5bhk/my-houses/ in parent-tax/child-tax/ format.

2) And also open this link they give

Oops! That page can’t be found

This link show this error display page not found. What the problem in my following function. Please help me its urgent.

  • @Jeff i read your ans and also use this in my function but i m facing two problem please help me if u r freeeeee – harry Feb 19 '18 at 9:55
0

I suspect the extra slash is coming from the str_replace action because normally there's no need to manually add slashes when retrieving TAX urls.

Since you now end up with 1 unwanted slash, I would recommend testing situations with/without the slash. Starting with removing the one in the function definition. Make it empty, see what happens.

That is the first step to look into, in my opinion. I think this post might help you a bit as well: https://codex.wordpress.org/Debugging_in_WordPress

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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