• Hi

    I created a plugin to set a custom post type and a custom taxonomy.
    Custom Post Type = Book
    Custom Taxonomy = Genre
    Custom Terms = Fantasy, Horror

    I would like to set these permalinks:
    – mydomain.com/books (to list all the posts with book as post type)
    – mydomain.com/books/fantasy (to list all the books using the term fantasy)
    – mydomain.com/books/the-hobbit (to show the individual post of the book “the-hobbit”)

    It’s slighly like this topic, except that for me mydomain.com/books is working, mydomain.com/books/fantasy is working and it’s mydomain.com/books/the-hobbit which is not working. If I list all my posts & books on my homepage, the link is correctly generated but I get a 404 error if I go on the page.

    I followed this tutorial but it’s not working

    My code:

    function wptuts_custom_tags() {
        add_rewrite_rule("^books/([^/]+)/([^/]+)/?",'index.php?post_type=book&genre=$matches[1]&book=$matches[2]','top');
    }
    add_action('init','wptuts_custom_tags');  
    
    //CREATE CUSTOM TAXONOMIES
    
    function create_books_taxonomies(){
    	$labels = array(
    		'name' => 'Book Genre',
    		'add_new_item' => 'Add New Book Genre',
    		'new_item_name' => 'New Book Genre'
    	);
    
    	$args = array(
    		'labels' => $labels,
    		'show_ui' => true,
    		'hierarchical' => false,
    		//'rewrite' => true,
    		'rewrite' => array(
    			'slug'=>'books',
    			'with_front'=>'false'
    		),
    		'show_tagcloud' => true
    	);
    	register_taxonomy( 'genre', 'books', $args);
    }
    
    add_action( 'init', 'create_books_taxonomies');
    
    // CREATE CUSTOM POST TYPES
    
    function create_book()
    {
    	$labels = array(
    		'name' => 'Books',
    		'singular_name' => 'Book',
    		'add_new' => 'Add New',
    		'add_new_item' => 'Add New Book',
    		'edit' => 'Edit',
    		'edit_item' => 'Edit Book',
    		'new_item' => 'New Book',
    		'view' => 'View',
    		'view_item' => 'View Book',
    		'search_items' => 'Search Books',
    		'not_found' => 'No Books found',
    		'not_found_in_trash' => 'No Books found in Trash',
    		'parent' => 'Parent Book'
    	);  
    
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'menu_position' => 15,
    		'supports' => array( 'title', 'editor', 'comments', 'thumbnail',  ),
    		'rewrite' => array(
    			'slug'=>'books',
    			'with_front'=> false,
    			'feed'=> true,
    			'pages'=> true
    		),
    		'has_archive'=>true,
    		'query_var' => true
    	);
    	register_post_type( 'books', $args);
    }
    add_action( 'init', 'create_book' );
    
    function wptuts_book_link( $post_link, $id = 0 ) {
        $post = get_post($id);  
    
        if ( is_wp_error($post) || 'book' != $post->post_type || empty($post->post_name) )
            return $post_link;  
    
        // Get the genre:
        $terms = get_the_terms($post->ID, 'genre');
        if( is_wp_error($terms) || !$terms ) {
            $genre = 'uncategorised';
        } else {
            $genre_obj = array_pop($terms);
            $genre = $genre_obj->slug;
        }
    	return home_url(user_trailingslashit( "books/$post->post_name" ));
    }
    add_filter( 'post_type_link', 'wptuts_book_link' , 10, 2 );  
    
    // --- FLUSH ---
    function wptuts_plugin_activation() {
        create_book();
        flush_rewrite_rules();
    }
    register_activation_hook( __FILE__, 'wptuts_plugin_activation');  
    
    function wptuts_plugin_deactivation() {
        flush_rewrite_rules();
    }
    register_activation_hook( __FILE__, 'wptuts_plugin_activation'); 
    
    add_action('admin_init', 'flush_rewrite_rules');

    Do you know what could be wrong or if I should use another solution?
    Thank you

Viewing 1 replies (of 1 total)
  • Sh14

    (@sh14)

    I have the same problem!
    I tried to solve problem with permalink by many ways, but didn’t do this yet.

    If anybody have solution, please, help us!

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Post Type, Taxonomy and Permalinks’ is closed to new replies.