admin管理员组

文章数量:1287527

I have created a custom post type "events" with a custom taxonomy "event-types". Single pages for the event posts work fine. Archive page of the events works fine.

However when I have the term "adults" I expect this page to take me to an archive of all the events with the term adults:

/

This just takes me to a "Nothing Found" (I guess 404?) page.

But this link takes me to the exact page that I want:

/?post_type=events

I don't want to have the ?post_type=events in the url. I don't even know why it is there, since I thought it should just automatically work without it.

Here is my cpt code:

add_action( 'init', 'cdf_events_register_post_type' );
function cdf_events_register_post_type() {
    $args = [
        'label'  => esc_html__( 'CDF Events', 'text-domain' ),
        'labels' => [
            'menu_name'          => esc_html__( 'CDF Events', 'cdf-serene' ),
            'name_admin_bar'     => esc_html__( 'CDF Event', 'cdf-serene' ),
            'add_new'            => esc_html__( 'Add CDF Event', 'cdf-serene' ),
            'add_new_item'       => esc_html__( 'Add new CDF Event', 'cdf-serene' ),
            'new_item'           => esc_html__( 'New CDF Event', 'cdf-serene' ),
            'edit_item'          => esc_html__( 'Edit CDF Event', 'cdf-serene' ),
            'view_item'          => esc_html__( 'View CDF Event', 'cdf-serene' ),
            'update_item'        => esc_html__( 'View CDF Event', 'cdf-serene' ),
            'all_items'          => esc_html__( 'All CDF Events', 'cdf-serene' ),
            'search_items'       => esc_html__( 'Search CDF Events', 'cdf-serene' ),
            'parent_item_colon'  => esc_html__( 'Parent CDF Event', 'cdf-serene' ),
            'not_found'          => esc_html__( 'No CDF Events found', 'cdf-serene' ),
            'not_found_in_trash' => esc_html__( 'No CDF Events found in Trash', 'cdf-serene' ),
            'name'               => esc_html__( 'CDF Events', 'cdf-serene' ),
            'singular_name'      => esc_html__( 'CDF Event', 'cdf-serene' ),
        ],
        'public'              => true,
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'show_in_rest'        => true,
        'capability_type'     => 'post',
        'hierarchical'        => false,
        'has_archive'         => true,
        'query_var'           => true,
        'can_export'          => true,
        'rewrite_no_front'    => false,
        'show_in_menu'        => true,
        'menu_position'       => 6,
        'menu_icon'           => 'dashicons-calendar-alt',
        'supports' => [
            'title',
            'editor',
            'author',
            'thumbnail',
            'comments',
            'revisions',
            'page-attributes',
        ],
        
        'rewrite' => true
    ];

    register_post_type( 'events', $args );
}

And the custom taxonomy:

function event_types() {

    $labels = array(
        'name'                       => _x( 'Event Types', 'Taxonomy General Name', 'cdf_text' ),
        'singular_name'              => _x( 'Event Type', 'Taxonomy Singular Name', 'cdf_text' ),
        'menu_name'                  => __( 'Event Type', 'cdf_text' ),
        'all_items'                  => __( 'All Items', 'cdf_text' ),
        'parent_item'                => __( 'Parent Event Type', 'cdf_text' ),
        'parent_item_colon'          => __( 'Parent Event Type:', 'cdf_text' ),
        'new_item_name'              => __( 'New Event Type', 'cdf_text' ),
        'add_new_item'               => __( 'Add New Event Type', 'cdf_text' ),
        'edit_item'                  => __( 'Edit Event Type', 'cdf_text' ),
        'update_item'                => __( 'Update Event Type', 'cdf_text' ),
        'view_item'                  => __( 'View Event Type', 'cdf_text' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'cdf_text' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'cdf_text' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'cdf_text' ),
        'popular_items'              => __( 'Popular Items', 'cdf_text' ),
        'search_items'               => __( 'Search Items', 'cdf_text' ),
        'not_found'                  => __( 'Not Found', 'cdf_text' ),
        'no_terms'                   => __( 'No items', 'cdf_text' ),
        'items_list'                 => __( 'Items list', 'cdf_text' ),
        'items_list_navigation'      => __( 'Items list navigation', 'cdf_text' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'has_archive'                => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_rest'               => true
    );
    register_taxonomy( 'event-types', array( 'events' ), $args );

}
add_action( 'init', 'event_types', 0 );

EDIT: I am refreshing the permalinks by saving the settings each time I make a change.

I'll also note that when I go to the backend page to edit/add terms and I hover over the term "adults", it gives me the option to view and takes me to the non-working link /

I have created a custom post type "events" with a custom taxonomy "event-types". Single pages for the event posts work fine. Archive page of the events works fine.

However when I have the term "adults" I expect this page to take me to an archive of all the events with the term adults:

https://website/event-types/adults/

This just takes me to a "Nothing Found" (I guess 404?) page.

But this link takes me to the exact page that I want:

https://website/event-types/adults/?post_type=events

I don't want to have the ?post_type=events in the url. I don't even know why it is there, since I thought it should just automatically work without it.

Here is my cpt code:

add_action( 'init', 'cdf_events_register_post_type' );
function cdf_events_register_post_type() {
    $args = [
        'label'  => esc_html__( 'CDF Events', 'text-domain' ),
        'labels' => [
            'menu_name'          => esc_html__( 'CDF Events', 'cdf-serene' ),
            'name_admin_bar'     => esc_html__( 'CDF Event', 'cdf-serene' ),
            'add_new'            => esc_html__( 'Add CDF Event', 'cdf-serene' ),
            'add_new_item'       => esc_html__( 'Add new CDF Event', 'cdf-serene' ),
            'new_item'           => esc_html__( 'New CDF Event', 'cdf-serene' ),
            'edit_item'          => esc_html__( 'Edit CDF Event', 'cdf-serene' ),
            'view_item'          => esc_html__( 'View CDF Event', 'cdf-serene' ),
            'update_item'        => esc_html__( 'View CDF Event', 'cdf-serene' ),
            'all_items'          => esc_html__( 'All CDF Events', 'cdf-serene' ),
            'search_items'       => esc_html__( 'Search CDF Events', 'cdf-serene' ),
            'parent_item_colon'  => esc_html__( 'Parent CDF Event', 'cdf-serene' ),
            'not_found'          => esc_html__( 'No CDF Events found', 'cdf-serene' ),
            'not_found_in_trash' => esc_html__( 'No CDF Events found in Trash', 'cdf-serene' ),
            'name'               => esc_html__( 'CDF Events', 'cdf-serene' ),
            'singular_name'      => esc_html__( 'CDF Event', 'cdf-serene' ),
        ],
        'public'              => true,
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'show_in_rest'        => true,
        'capability_type'     => 'post',
        'hierarchical'        => false,
        'has_archive'         => true,
        'query_var'           => true,
        'can_export'          => true,
        'rewrite_no_front'    => false,
        'show_in_menu'        => true,
        'menu_position'       => 6,
        'menu_icon'           => 'dashicons-calendar-alt',
        'supports' => [
            'title',
            'editor',
            'author',
            'thumbnail',
            'comments',
            'revisions',
            'page-attributes',
        ],
        
        'rewrite' => true
    ];

    register_post_type( 'events', $args );
}

And the custom taxonomy:

function event_types() {

    $labels = array(
        'name'                       => _x( 'Event Types', 'Taxonomy General Name', 'cdf_text' ),
        'singular_name'              => _x( 'Event Type', 'Taxonomy Singular Name', 'cdf_text' ),
        'menu_name'                  => __( 'Event Type', 'cdf_text' ),
        'all_items'                  => __( 'All Items', 'cdf_text' ),
        'parent_item'                => __( 'Parent Event Type', 'cdf_text' ),
        'parent_item_colon'          => __( 'Parent Event Type:', 'cdf_text' ),
        'new_item_name'              => __( 'New Event Type', 'cdf_text' ),
        'add_new_item'               => __( 'Add New Event Type', 'cdf_text' ),
        'edit_item'                  => __( 'Edit Event Type', 'cdf_text' ),
        'update_item'                => __( 'Update Event Type', 'cdf_text' ),
        'view_item'                  => __( 'View Event Type', 'cdf_text' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'cdf_text' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'cdf_text' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'cdf_text' ),
        'popular_items'              => __( 'Popular Items', 'cdf_text' ),
        'search_items'               => __( 'Search Items', 'cdf_text' ),
        'not_found'                  => __( 'Not Found', 'cdf_text' ),
        'no_terms'                   => __( 'No items', 'cdf_text' ),
        'items_list'                 => __( 'Items list', 'cdf_text' ),
        'items_list_navigation'      => __( 'Items list navigation', 'cdf_text' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'has_archive'                => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_rest'               => true
    );
    register_taxonomy( 'event-types', array( 'events' ), $args );

}
add_action( 'init', 'event_types', 0 );

EDIT: I am refreshing the permalinks by saving the settings each time I make a change.

I'll also note that when I go to the backend page to edit/add terms and I hover over the term "adults", it gives me the option to view and takes me to the non-working link https://website/event-types/adults/

Share Improve this question edited Sep 21, 2021 at 10:47 HopefullCoder asked Sep 21, 2021 at 8:52 HopefullCoderHopefullCoder 456 bronze badges 4
  • Apologies if I haven't given all the needed info, or if I have included too much code. I'm still new at this. – HopefullCoder Commented Sep 21, 2021 at 8:53
  • Your taxonomy is registered before the post type, if you change the priority of the event_types callback to 11 (from 0) does it make a difference? Make sure to refresh permalinks too. – Jacob Peattie Commented Sep 21, 2021 at 9:02
  • @JacobPeattie So you mean change it to add_action( 'init', 'event_types', 11 );? Doing that didn't change the situation. Other ideas? Thanks! – HopefullCoder Commented Sep 21, 2021 at 9:10
  • 1 just a note that the default priority is 10, which is why Jacob suggested 11. WP executes lowest priority number first, highest priority number last, 1,2,3....20,21,22.. etc – Tom J Nowell Commented Sep 21, 2021 at 10:07
Add a comment  | 

1 Answer 1

Reset to default 1

After hours of working on this, the answer was simple

Change this to false:

'exclude_from_search' => false,

Then it works as expected.

本文标签: Custom taxonomy archive page requires posttype in url