Support » Developing with WordPress » Archive page for CPT tags with rewrite URL

  • Hi everybody, i’ve been looking for an answer for days, i hope you can help me!

    I created a custom post type, name ‘movie’ with rewrite => slug ‘films’.
    I created a page with the slug ‘films’ so i can customize the page as i want and display the content of my CPT ‘movie’. It works fine.
    I registered a tag taxonomy for the CPT, named ‘movie_tag’, width the slug ‘films/tags’. I can access my movie tag trough the url “xxx.com/films/tags/my_tag”.
    I’d like to access a custom page, containing every tags (movie_tag) used with my CPT ‘movie’ and display them on this “archive” page, but i don’t know how.

    Here is my code, did i forgot something? I tried naming several pages like archive-movie_tag.php taxonomy-movie_tag.php, even archive.php and tag.php but each time i accessed “xxx.com/films/tags/” i went to the index.php.

    register_taxonomy(
            'movie_tag',
            'movie',
                array(
                    'label' => 'Tags',
                    'labels' => array(
                        'name' => 'Tags',
                        'singular_name' => 'Tag',
                        'all_items' => 'Tous les tags',
                        'edit_item' => 'Éditer le tag',
                        'view_item' => 'Voir le tag',
                        'update_item' => 'Mettre à jour le tag',
                        'add_new_item' => 'Ajouter un tag',
                        'new_item_name' => 'Nouveau tag',
                        'search_items' => 'Rechercher parmi les tag',
                        'popular_items' => 'Tags les plus utilisés'
                    ),
                    'hierarchical' => false,
                    'has_archive' => false,
                    'capability_type' => 'page',
                    'public' => true,
                    'rewrite' => array(
                        'slug' => 'films/tags',
                        'with_front' => false,
                    ),
                )
            );
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello @gwenad,

    With custom taxonomy it doesn’t work like that. I mean it will not list all taxonomy terms on a URL films/tags. WordPress doesn’t make taxonomy terms listing URL by default like custom post type. For custom post type “films” slug will list all movies but with custom taxonomy “films/tags” will not list all tags because wordpress doesn’t do it automatically.

    If you want to list all taxonomy you may need to create a custom page template and in that you have to code it with get_terms('movie_tag', array('hide_empty' => false)); https://developer.wordpress.org/reference/functions/get_terms/

    I hope this helps.

    Thanks.

    Thx for your answer Gaurav Padia,

    I know that’s the way to do it, i already did it for my CPT category (named ‘movie_category’) and it works, but for some weird reason, it doesn’t work for the tags !

    I created a page with the slug “xxx.com/films/tags” (with the loop and get_terms), created a CPT tags with the rewrite slug ‘films/tags’ and ‘has_archive’ => ‘films/tags’ (the slug of the previously created page) but each time i access xxx.com/films/tag, it drives me to the index.php (even not archive.php or tag.php or the template of the page.

    Any idea ?

    Full code of my custom post type, if it can help somebody…

    register_taxonomy(
            'movie_category',
            'movie',
                array(
                    'label' => 'Categories',
                    'labels' => array(
                        'name' => 'Catégories',
                        'singular_name' => 'Catégorie',
                        'all_items' => 'Toutes les catégories',
                        'edit_item' => 'Éditer la catégorie',
                        'view_item' => 'Voir la catégorie',
                        'update_item' => 'Mettre à jour la catégorie',
                        'add_new_item' => 'Ajouter une catégorie',
                        'new_item_name' => 'Nouvelle catégorie',
                        'search_items' => 'Rechercher parmi les catégories',
                        'popular_items' => 'Catégorie les plus utilisées'
                    ),
                    'show_admin_column' => true,
                    'hierarchical' => true,
                    'has_archive' => false,
                    'capability_type' => 'page',
                    'public' => true,
                    'query_var' => true,
                    'rewrite' => array(
                        'slug' => 'films',
                        'with_front' => false,
                    ),
                )
            );
    
            register_taxonomy(
            'movie_tag',
            'movie',
                array(
                    'label' => 'Tags',
                    'labels' => array(
                        'name' => 'Tags',
                        'singular_name' => 'Tag',
                        'all_items' => 'Tous les tags',
                        'edit_item' => 'Éditer le tag',
                        'view_item' => 'Voir le tag',
                        'update_item' => 'Mettre à jour le tag',
                        'add_new_item' => 'Ajouter un tag',
                        'new_item_name' => 'Nouveau tag',
                        'search_items' => 'Rechercher parmi les tag',
                        'popular_items' => 'Tags les plus utilisés'
                    ),
                    'show_admin_column' => true,
                    'hierarchical' => false,
                    'has_archive' => 'films/tags',
                    'capability_type' => 'page',
                    'public' => true,
                    'query_var' => true,
                    'rewrite' => array(
                        'slug' => 'films/tags',
                        'with_front' => false,
                    ),
                )
            );
    
            register_post_type(
                'movie',
                array(
                    'label' => 'Films',
                    'labels' => array(
                        'name' => 'Films',
                        'singular_name' => 'Film',
                        'all_items' => 'Tous les films',
                        'add_new_item' => 'Ajouter un film',
                        'edit_item' => 'Éditer le film',
                        'new_item' => 'Nouveau film',
                        'view_item' => 'Voir le film',
                        'search_items' => 'Rechercher parmi les films',
                        'not_found' => 'Pas de film trouvé',
                        'not_found_in_trash'=> 'Pas de film dans la corbeille'
                    ),
                    
                    'public' => true,
                    'has_archive' => false,
                    'rewrite' => array(
                        'slug' => 'films/%movie_category%',
                        'with_front' => false,
                    ),
                )
            );
    Moderator bcworkz

    (@bcworkz)

    It’s because the rewrite rule for film tags is being applied instead of simply getting the page. If you rename your page something like all-film-tags, the page will be retrieved and its template code will execute.

    For whatever reason, your movie category page is not picked up by the wrong rewrite rule, so it works fine.

    I’d also suggest (as an individual forum member, not as moderator) in future posts you not use triple x as a fill-in domain (assuming it’s not your actual domain). I’ve not verified it, but I’m pretty sure it’s a functional domain that a lot of people would rather not visit, especially at work (I expect it has ‘pr0n’ content). Fortunately, the forum’s parser did not linkify it like it often does. It’s generally suggested we use example.com as a fill-in domain.

    I just found something, in a standard wordpress them, there is no tag archive template. WordPress doesn’t seem to manage this kind of archive.
    Am i wrong?
    So i wanted to build something impossible?!

    Hello @gwenad,

    Yes, there is no archive for tags itself. Even in your code “register_taxonomy” you have added an argument “has_archive” but there is no such argument https://codex.wordpress.org/Function_Reference/register_taxonomy. “has_arhive” is for “register_post_type”

    Do you know why “films/tags” URL page is not showing ? That is because you have registered slug “films” for movie_category taxonomy so wordpress actually looks for category named “tags” in database which is not present and you are seeing 404 page.

    But that doesn’t mean it is impossible. You have to learn URL rewriting with wordpress. https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

    Like to show page on “films/tags” use this code and change the page_id=185 to your page id:

    
    function wpd_page_rewrite(){
        add_rewrite_rule(
            '^films/tags/?$',
            'index.php?page_id=185',
            'top'
        );
    }
    add_action( 'init', 'wpd_page_rewrite' );
    

    After adding you have to flush rewrite urls using function “flush_rewrite_rules” https://codex.wordpress.org/Function_Reference/flush_rewrite_rules

    As soon as rewrite rules are updated you will see your page for tags. So I think you will need to apply rewrite rules to work as you want.

    I hope this helps.

    Thanks.

    • This reply was modified 3 years, 1 month ago by Gaurav Padia.
    • This reply was modified 3 years, 1 month ago by Gaurav Padia.
    Moderator bcworkz

    (@bcworkz)

    I’ve one thing to add to Gaurav’s excellent reply in case it’s not clear. Any time a theme does not contain a specific archive template, such as for tags, or even categories or any other custom post or taxonomy a theme might implement; WP will still first try to find such a template, like category.php or tag.php. When no such template is available, WP will try to use archive.php. If even that is not available, WP will use index.php. (not the same index.php that is used for rewrite rules, that is a WP file in installation root.) For more details, see Template Hierarchy.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Archive page for CPT tags with rewrite URL’ is closed to new replies.