WordPress.org

Ready to get started?Download WordPress

Forums

Custom post type & custom category taxonomy not working (9 posts)

  1. slaaxy
    Member
    Posted 2 years ago #

    Hi, Ive got a custom post type running and made a custom category for it. Everything works great except for the url to the category which I want to show after the post type slug.

    example.com/games (works)
    example.com/games/genre/category (doesn't work)
    example.com/genre/category (works)

    Here is my code:

    function my_custom_post() {
    	register_post_type( 'games',
    		array(
    			'labels' => array(
    				'name' => __( 'Games' ),
    				'singular_name' => __( 'Game' ),
    			),
    
    		'public' => true,
    		'show_ui' => true,
    		'hierarchical' => false,
    		'has_archive' => true,
    		'capability_type' => 'post',
    		'supports' => array( 'title', 'excerpt', 'thumbnail' ),
    		'taxonomies' => array( 'genre'),
    		'rewrite' => array('slug' => 'games','with_front' => FALSE),
    		)
    	);
    
    }
    
    function my_custom_txonomy() {
       register_taxonomy(
        'games',
        'games_category',
        array(
            'hierarchical' => true,
            'label' => 'Games categories',
            'query_var' => true,
            'rewrite' => array('slug' => 'genre','with_front' => false)
        )
    ); 
    
    } 
    
    add_action('init', 'my_custom_post', 0 );
    add_action('init', 'my_custom_txonomy', 10);
  2. slaaxy
    Member
    Posted 2 years ago #

    Okay so I have tested just about every thing in article out there that is supposed to fix this. And nothing works. Not really sure where I am wrong, would appreciate if someone could give me a few pointers.

  3. birgire
    Member
    Posted 2 years ago #

  4. slaaxy
    Member
    Posted 2 years ago #

    Yes, that one and lots of others, most of them seem to be using the same slug for both the post type and the taxonomy which for me results in a 404. I did at one point get it to work for the custom taxonomy but then the custom post type 404'ed

    so I had example.com/post_type/categorey (working)
    example.com/post_type (not working)
    example.com/post_type/post (not working)

  5. birgire
    Member
    Posted 2 years ago #

    to check the current supported rewrite rules, one can use:

    <pre>
    <?php
    global $wp_rewrite;
    print_r($wp_rewrite->rules);
    ?>
    </pre>

    (very long list)

  6. birgire
    Member
    Posted 2 years ago #

    this is a handy plugin to test the rewrite mess:

    http://wordpress.org/extend/plugins/monkeyman-rewrite-analyzer/screenshots/

  7. slaaxy
    Member
    Posted 2 years ago #

    Thanks for the information, I have given up on it for now.

  8. birgire
    Member
    Posted 2 years ago #

    ok, just to let you know that this is working for me to display

    example.com/cpt/tax/taxvalue

    or

    example.com/games/genre/category

    in your case.

    I add this into functions.php

    function my_custom_rewrite_rules( $rules ) {
            $newrules = array();
    
            $cpt="games"; // your custom post type query_var name
            $tax="genre"; // your custom taxonomy query_var name
    
            $newrules[$cpt.'/'.$tax.'/(.*)$'] = 'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]';
    
            return $newrules + $rules;
    }
    add_filter('rewrite_rules_array','my_custom_rewrite_rules');

    and then press save on the permalinks page in the wp-admin to flush the rewrite rules.

  9. Ryan
    Member
    Posted 1 year ago #

    I tried birgire's solution, but didn't have any luck.

    I ended up using the Custom Post Type Permalinks plugin. Makes it dead easy.

Topic Closed

This topic has been closed to new replies.

About this Topic