• Hi,

    I made and checked the post type page archive-videos.php. However, I created taxonomy-videos.php to load the page registered in the added taxonomy, but the page could not be found. What was the solution?

    I would like to know which files should be created and which settings should be added.

    The problem now is that my_domain/videos is well expressed as archive-videos.php, but I want my_domain/videos/category-slug to be expressed according to each taxonomy.

    my source here

    add_action( 'init', 'set_rs_custom_posts' );
    function set_rs_custom_posts() {
        $news_label = array(
            'name'               => __('News', 'rsupport' ),
            'singular_name'      => __('News', 'rsupport' ),
            'menu_name'          => __('News', 'rsupport'),
            'show_in_nav_menus' => true,
            'show_in_menu' => true,
        );
    
        $news = array(
            'labels'        => $news_label,
            'description'   => __('news', 'rsupport'),
            'public'        => true,
            'menu_position' => 5,
            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'page-attributes'),
            'has_archive'   => true, // only this is required to enable archive page else 404 error will occur
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'hierarchical' => false,
            // 'taxonomies'  => array( 'news', 'news-category' ),
            
        );
        $video_label = array(
            'name'               => __('Video', 'rsupport' ),
            'singular_name'      => __('Video', 'rsupport' ),
            'menu_name'          => __('Video', 'rsupport'),
            'show_in_nav_menus' => true,
            'show_in_menu' => true,
        );
    
        $video = array(
            'labels'        => $video_label,
            'description'   => __('video', 'rsupport'),
            'public'        => true,
            'menu_position' => 5,
            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'page-attributes'),
            'has_archive'   => true, // only this is required to enable archive page else 404 error will occur
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'hierarchical' => false,
            'taxonomies' => array('videos'),
            // 'rewrite' => true,
            'rewrite' => array( 'slug' => 'videos','with_front' => false),
        );
    
        // //register_post_type( 'video', $video );
        // register_post_type( 'videoguide', $video );
        register_post_type( 'videos', $video );
        register_post_type( 'news', $news );
    
        flush_rewrite_rules();
    }
    
    add_action( 'init', 'set_rs_custom_categories' );
    function set_rs_custom_categories(){
        $taxonomy_labels = array(
            'name'                       => _x( 'Category', 'Taxonomy General Name', 'rsupport' ),
            'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'rsupport' ),
            'menu_name'                  => __( 'Category', 'rsupport' ),
            'all_items'                  => __( 'All Items', 'rsupport' ),
            'parent_item'                => __( 'Parent Item', 'rsupport' ),
            'parent_item_colon'          => __( 'Parent Item:', 'rsupport' ),
            'new_item_name'              => __( 'New Item Name', 'rsupport' ),
            'add_new_item'               => __( 'Add New Item', 'rsupport' ),
            'edit_item'                  => __( 'Edit Item', 'rsupport' ),
            'update_item'                => __( 'Update Item', 'rsupport' ),
            'separate_items_with_commas' => __( 'Separate items with commas', 'rsupport' ),
            'search_items'               => __( 'Search Items', 'rsupport' ),
            'add_or_remove_items'        => __( 'Add or remove items', 'rsupport' ),
            'choose_from_most_used'      => __( 'Choose from the most used items', 'rsupport' ),
            'not_found'                  => __( 'Not Found', 'rsupport' ),
        );
    
        $args_news = array(
            'labels'                     => $taxonomy_labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
            'query_var'                  =>true,
            'has_archive'                => true,
            'rewrite' => array( 'slug' => 'news'),
        );
        $args_videos = array(
            'labels'                     => $taxonomy_labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
            'query_var'                  =>true,
            'has_archive'                => true,
            'rewrite' => array( 'slug' => 'videos','with_front' => false),
        );
    
        register_taxonomy( 'news', 'news', $args_news );
        register_taxonomy( 'videos', 'videos', $args_videos );
        
    }
    
    add_action( 'init', 'set_rs_custom_register_taxonomy' );
    function set_rs_custom_register_taxonomy() {
        register_taxonomy_for_object_type( 'news', 'news' );
        register_taxonomy_for_object_type( 'videos', 'videos' );
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter gnjang

    (@gnjang)

    function taxonomy_slug_rewrite($wp_rewrite) {
        $rules = array();
    
        $taxonomies = get_terms( array(
            'taxonomy' => 'videos',
            'hide_empty' => false,
        ) );
    
        $post_types = get_post_types(
            array('public' => true, '_builtin' => false), 
            'videos'
        );
    
        foreach ($post_types as $post_type) {
            foreach ($taxonomies as $taxonomy) {
    
                if ($taxonomy->taxonomy != $post_type->name) continue;
    
                    $categories = get_categories(array(
                        'type' => $post_type->capability_type, 
                        'taxonomy' => $taxonomy->taxonomy, 
                        'hide_empty' => 0
                    ));
    // echo "<pre>";
    // print_r($post_type->name);                
    // print_r($taxonomy->taxonomy);                
    // print_r($categories);                
                    foreach ($categories as $category) {
                        $rules[$post_type->name . '/' . $category->slug . '/?$'] = 
                            // 'index.php?post_type=' . $post_type->name .'&'. $category->taxonomy . '=' . $category->slug;
                            'index.php?post_type=' . $post_type->name .'&category_name=$matches[1]&feed=$matches[2]';
    
                    }
            } // taxonomies
        } // post_types
        $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    echo "<pre>";
    print_r($wp_rewrite);
    die;
    }

    I’m trying rewite with that source.
    But I want to link to taxonomy-videos.php but it doesn’t work.

    • This reply was modified 2 years, 5 months ago by gnjang.
    • This reply was modified 2 years, 5 months ago by bcworkz. Reason: code format fixed
    Moderator bcworkz

    (@bcworkz)

    You cannot have identical rewrite slugs for both taxonomy and post type. One of them needs to be changed. A taxonomy query is very different than a posts query. WP will not know which query to make if they both use the same rewrite slug. They can be very similar, just not identical. For example “video” and “videos”.

    Thread Starter gnjang

    (@gnjang)

    Hi @bcworkz

    Then, if post slug is video and taxonomy is video, is taxonomy read in taxonomy-video-videos.php? I’m going to give it a try.Then, if post slug is video and taxonomy is video, is taxonomy read in taxonomy-video-videos.php? I tried both taxonomy-video-videos and taxonomy-video but failed.
    I want the /video/category-slug url to work.

    • This reply was modified 2 years, 5 months ago by gnjang.
    Moderator bcworkz

    (@bcworkz)

    If the same “video” rewrite slug for two different object types is assigned (it shouldn’t be), it’s ambiguous what a /video/whatever/ URL would be processed for. Is the request for a post or a taxonomy term archive? No way to know ahead of time until a query is made. Even worse is if a post and term also have the same slug.

    I suspect WP in this case will assume a post type over taxonomy, so archive-video.php would be the template used (unverified). In this case, the video taxonomy is essentially inaccessible, so it doesn’t matter what taxonomy-video.php does, it will not be used. Any video taxonomy term request would be treated as post requests and probably go 404.

    There needs to be something in the URL that distinguishes one type of request from the other without needing to make a test query.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom post type & custom taxonomy page load’ is closed to new replies.