Make WordPress Core

Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#16233 closed defect (bug) (worksforme)

Custom taxonomy archive returns 404 when it contains only custom post types

Reported by: sillybean Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords: dev-feedback
Focuses: Cc:

Description (last modified by dd32)

I'm working with a few custom taxonomies that are registered only for a custom post type, not posts or pages. The archive pages return 404s unless I add posts or pages to the taxonomy's content object argument.

Here's the post type:

		'labels' => array(
		'name' => __( 'Courses' ),
		'singular_name' => __( 'Course' ),
		'add_new' => __('Add New'),
		'add_new_item' => __('Add New Course'),
		'edit_item' => __('Edit Course'),
		'new_item' => __('New Course'),
		'view_item' => __('View Course'),
		'search_items' => __('Search Courses'),
		'not_found' => __('No courses found'),
		'not_found_in_trash' => __('No courses found in Trash'),
		'menu_name' => __('Courses'),
	'capability_type' => 'post',
	'map_meta_cap' => true,
	'capabilities' => array(
		'edit_post' => 'edit_course',
		'read_post' => 'read_course',
		'delete_post' => 'delete_course',
	'description' => __('Individual course data'),
	'public' => true, 
	'show_ui' => true,
	'hierarchical' => false,
	'menu_icon' => get_bloginfo('template_url').'/images/bank.png',
	'register_meta_box_cb' => 'course_meta_boxes',
	'supports' => array(

And one of the taxonomies:

		'labels' => array(
		'name' => __( 'Colleges' ),
		'singular_name' => __( 'College' ),
		'search_items' => __( 'Search Colleges' ),
		'popular_items' => __( 'Popular Colleges' ),
		'all_items' => __( 'All Colleges' ),
		'parent_item' => __( 'Parent College' ),
		'parent_item_colon' => __( 'Parent College:' ),
		'edit_item' => __( 'Edit College' ),
		'update_item' => __( 'Update College' ),
		'add_new_item' => __( 'Add New College' ),
		'new_item_name' => __( 'New College Name' ),
		'separate_items_with_commas' => __( 'Separate colleges with commas' ),
		'add_or_remove_items' => __( 'Add or remove colleges' ),
		'choose_from_most_used' => __( 'Choose from the most used colleges' ),
		'menu_name' => __('Colleges'), 
	'hierarchical' => true,

Create a few courses, assign them to colleges, and the archives (e.g. /college/liberal-arts) don't work.

If I change the taxonomy definition to include posts (or pages), the archive suddenly work.

		'labels' => array(),
	'hierarchical' => true,

Change History (14)

#1 @sillybean
5 years ago

ARGH, sorry about the formatting. I thought I hit Preview. :(

#2 @dd32
5 years ago

  • Description modified (diff)

Just updating the ticket formatting.

#3 @dd32
5 years ago

  • Keywords dev-feedback 3.2-early added
  • Milestone changed from Awaiting Review to Future Release

This is "by design" for loops, custom post types don't show in the standard loop, or archive loops.

Taxonomies default to listing posts only, If it applies to other post types, then they're skipped unless you add it by hooking and modifying the queryvars on load.

In my opinion, If a custom taxonomy is registered as public, then it should be querying all registered post types to that taxonomy.

Date Timeline archive views are different, and like pages, custom post types should be excluded as currently happens.

#4 @sillybean
5 years ago

Thank you for fixing the formatting. :) And yes, I expected that the taxonomy archive would show all the registered post types, even if the date-based archives do not.

#5 @dd32
5 years ago

Ok, It's just been pointed out to me, that i'm wrong.

Taxonomy archives *should* be returning posts in "any" post types, that excludes certain internal post types (is_public = false i think)

Let me try this out..

#6 @scribu
5 years ago

  • Cc scribu added

#7 @WraithKenny
5 years ago

Had a similar issue... If you just need it working, I think the following should work:

add_action( 'pre_get_posts', 'on_pre_get_posts' );
function on_pre_get_posts() {
    global $wp_query;	
    if ( is_tax( 'college' ) )
        $wp_query->set(  'post_type', array( 'any' ) );

#8 @sillybean
5 years ago

Oddly enough, this also works:

  1. Create the taxonomy and assign it to both the CPT and a built-in type (posts).
  2. Activate plugin.
  3. Without doing anything else, deactivate plugin.
  4. Remove the built-in type, and reactivate with only the CPT.

Archives work after that.

This has the feel of a caching problem, maybe related to #16182?

#9 @neoxx
5 years ago

  • Cc neo@… added

#10 @neoxx
5 years ago

I think it should be $wp_query->set( 'post_type', 'any' ); - any as an array does not work for me (latest trunk), though (custom-)post-types do.

Can anybody confirm this?

#11 @azizur
5 years ago

  • Cc azizur added

#12 @wonderboymusic
3 years ago

  • Milestone Future Release deleted
  • Resolution set to fixed
  • Status changed from new to closed

Fixed here: [21855]#L2220

#13 @helen
3 years ago

  • Keywords 3.2-early removed
  • Milestone set to 3.5

#14 @SergeyBiryukov
3 years ago

  • Milestone 3.5 deleted
  • Resolution changed from fixed to worksforme

Doesn't look like [21855] made any difference for this.

I couldn't reproduce the original report in 3.1. As noted in comment:5, $post_type = 'any' was added for taxonomy queries in [15649] and [15650].

Note: See TracTickets for help on using tickets.