• I have this custom post type:

    add_action( 'init', 'ddcustom_create_post_types' );
    function ddcustom_create_post_types() {
    	register_post_type(
    		'mat',
    		array(
    			'has_archive'	=> true,
    			'labels'		=> array(
    				'name'					=> __( 'Food', 'dd-custom' ),
    				'singular_name'			=> __( 'Food', 'dd-custom' ),
    				'menu_name'				=> _x( 'Foods', 'admin menu', 'dd-custom' ),
    				'name_admin_bar'		=> _x( 'Food', 'add new on admin bar', 'dd-custom' ),
    				'add_new'				=> _x( 'Add new', 'mat', 'dd-custom' ),
    				'add_new_item'			=> __( 'Add new food', 'dd-custom' ),
    				'new_item'				=> __( 'New food', 'dd-custom' ),
    				'edit_item'				=> __( 'Edit food', 'dd-custom' ),
    				'view_item'				=> __( 'View food', 'dd-custom' ),
    				'all_items'				=> __( 'All foods', 'dd-custom' ),
    				'search_items'			=> __( 'Search foods', 'dd-custom' ),
    				'parent_item_colon'		=> __( 'Parent foods:', 'dd-custom' ),
    				'not_found'				=> __( 'No foods found.', 'dd-custom' ),
    				'not_found_in_trash'	=> __( 'No foods found in Trash.', 'dd-custom' )
    			),
    			'menu_icon' => 'dashicons-cart',
    			'public'		=> true,
    			'rewrite'		=> array(
    				// Prefixing with "dd-" to avoid clash with taxonomy which is used primarily
    				'slug'			=> 'dd-food'
    			),
    			'supports'		=> array(
    				'title',
    				'editor',
    				'thumbnail',
    				'excerpt',
    			),
    		)
    	);

    And then I register the following taxonomy:

    add_action( 'init', 'ddcustom_content_taxonomy' );
    function ddcustom_content_taxonomy() {
    	register_taxonomy(
    		'ratt',
    		'mat',
    		array(
    			'hierarchical' => true,
    			'labels' => array(
    				'name' 				=> _x( 'Food type', 'taxonomy general name', 'dd-custom' ),
    				'singular_name'		=> _x( 'Food type', 'taxonomy singular name', 'dd-custom' ),
    				'search_items'		=> __( 'Search food types', 'dd-custom', 'dd-custom' ),
    				'all_items'			=> __( 'All food types', 'dd-custom' ),
    				'parent_item'		=> __( 'Parent food type', 'dd-custom' ),
    				'parent_item_colon'	=> __( 'Parent food type:', 'dd-custom' ),
    				'edit_item'			=> __( 'Edit food type', 'dd-custom' ),
    				'update_item'		=> __( 'Update food type', 'dd-custom' ),
    				'add_new_item'		=> __( 'Add new food type', 'dd-custom' ),
    				'new_item_name'		=> __( 'New food type name', 'dd-custom' ),
    				'menu_name'			=> __( 'Food types', 'dd-custom' ),
    			),
    			'rewrite' => array(
    				'slug' => 'food'
    			),
    		)
    	);
    }

    The permalink structure is as follows: /%category%/%postname%/

    However, visiting example.com/food only loads the 404.php page. What am I doing wrong? Edit: the taxonomy is not empty, I have a few posts tagged in the taxonomy.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Look what happens when you set the default link structure, then use permalink structure you want

    Hi,

    Try to re-set permalinks in Settings –> Permalinks

    Thread Starter Niklas

    (@niklasbr)

    Tried it on your recommendation but neither example.com/?taxonomy=food nor example.com/food works. ??

    Thread Starter Niklas

    (@niklasbr)

    @markoof

    Thanks, but resetting didn’t work either, still loads the 404.php template file.

    Worth noting is that the following URL:s do work example.com/food/taxonomy-term where taxonomy-term is the slug any term in the taxonomy.

    Thread Starter Niklas

    (@niklasbr)

    I found this article on Smashing Magazine which has a very interesting section: Creating A Custom Landing Page For Taxonomy Archives (scroll down a bit). It looks just like what I might need, but I can’t get it working either:

    add_filter( 'taxonomy_archive ', 'slug_tax_page_one' );
    function slug_tax_page_one( $template ) {
    	if ( is_tax( 'ratt' ) ) { // 'ratt' is lowercase taxonomy name in register_taxonomy()
    		 global $wp_query;
    		 $page = $wp_query->query_vars['paged'];
    		if ( $page = 0 ) {
    			$template = get_stylesheet_directory(). '/taxonomy-food.php';
    		}
    	}
    
    	return $template;
    }

    What happens when you set hierarchical to false?
    Does the 404 go away then?

    Thread Starter Niklas

    (@niklasbr)

    @tribulant Software

    Thanks for your suggestion! I tried that, then I went to the Permalinks and re-saved, but it still does not work.

    Thread Starter Niklas

    (@niklasbr)

    Some clarification:

    This works: example.com/food/bagels and shows all posts with the bagels category from the custom taxonomy with food slug.

    However, example.com/food returns 404 not found.

    This the current configuration:

    register_taxonomy(
    	'ratt', // taxonomy id
    	array(
    		'mat' // the content type
    	),
    	array(
    		'hierarchical' => false,
    		'labels' => array(
    			// Removed for brevity
    		),
    		'public' => true,
    		'rewrite' => array(
    			'slug' => 'food',
    		),
    	)
    );

    @niklas have you figured out a solution? I’m getting the same issue.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom taxonomy returning 404 error’ is closed to new replies.