Hi guys
I have a custom post type "shop".
I have a taxonomy "products".
I have some terms : "sunglasses", "hat" etc...
I use taxonomy-products.php. I can see the sunglasses at the url "mysite.com/products/sunglasses" which use the template taxonomy-products.php
but where is the URL http://mysite.com/products using a template which list all products (all terms combined) ?
Yes I can use a page "products" with a template page... but it is not satisfactory. it makes a useless page in the backoffice.
Why there is no URL automatically generated (like mysite.com/products) for the archive of the taxo "products" ? or is there something I don't know?
I need this automatically URL, linked to a template.
Andrea P answers:
in the WP way of thinking, an archive of a Taxonomy doesn't have sense.
It might sound a bit weird at first but it does make sense if you follow their thoughts.
The page that you're looking for, would be an archive with "all posts which have any term from the taxonomy Products".
in the database a Taxonomy has relations with the terms, and the terms have relations with the posts. A single post doesn't have relations with the taxonomy. The only thing that connect them is the fact that a post-type could support a Taxonomy.
Let's write down in words what you're trying to display in that page: "Show me all the posts of the post_type 'shop', which has ANY term from the taxonomy Products."
this basically translate to: "show me all posts of the post_type 'shop' (which supports the taxonomy Products)".
Therefore the only difference from a general post_type archive (show all posts from a given post_type) would be IF you have posts which don't have any Product term assigned and you want to exclude them.
If that's the case, then you can create a custom page, make a generic query to all posts, then in the loop you check if the post has any Products term with wp_get_post_terms(get_the_ID(), 'prduct'), and eventually exclude it if doesn't have any.
otherwise another way for such page to make sense is if instead of listing posts, it would list term-names which link to the Term's archive. like a Category tree or a Terms List, maybe with each term having an image and a short excerpt. But in WP such page is not built by default, although it is not too difficult to build it with a custom Term_Query
Sébastien | French WordpressDesigner comments:
I'm not right with your explanations.
I could have several taxonomies... In this case, an archive for each taxonomy could be useful.
the Arnav solution allows to obtain roughly the expected result but only if there is a single taxonomy
Sébastien | French WordpressDesigner comments:
And I had already seen "your" answer a few hours ago on ttps://wordpress.stackexchange.com/a/316133
In this page, the response of Pixelbbacon seems to me better : "Basically, like many people, you want a get_taxonomy_archive_link method.
Except, for one reason or another, it simply does not exist. I consider this a MAJOR failure on Wordpress's side."
Andrea P comments:
Hi Sebastien,
first of all, that answer in wp-stackexchange is mine. When I saw your question I remembered I answered already a very similar question in the past and therefore I copied some bits from there to save time on writing my response. ;)
Then I am sorry but my answer was not meant to open a fight about what I like and what you like. I was just trying to explain how things works in wordpress and why they haven't made a page like that.
to be honest I do feel that there is some sense in their decision, but I can understand that in some cases it would make sense to have a page for such archive. Though probably in those case that page can be custom made fairly easily, therefore maybe it's not so bad that the wp developers haven't introduced it by default...
the thing is that in general even if you have more taxonomies, it's probably very difficult that you have posts which have only terms from one taxonomy and posts which have only terms from the other taxonomy. Usually if that's the case you would have 2 different post_types instead, to make the "parent" grouping, and then the taxonomies to do a more granular sub-grouping within each post types.. So in that case instead of a Taxonomy archive, you would use a post_type archive, which is supported by default in wp.
I mean that if you have a whole group of posts which will never have a term from Taxonomy A and will have only terms from Taxonomy B; and another group of posts which will never have Taxonomy B but only Taxonomy A, then you should probably re-organise your posts into 2 post_types, so then it's easy to list only the ones from post_type A (which have only Taxonomy A) and post_type B (which have only Taxonomy B)
It's all about deciding how to best organize the content, considering the structure and the functionality of the framework that you're using. ;)
Andrea P comments:
In case you need it, in order to make a wp_query which pulls all the posts which have any term from a specific taxonomy, you can simply do something like this:
$terms = get_terms( array(
'taxonomy' => 'products',
'hide_empty' => false,
'fields' => 'ids'
) );
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'products',
'field' => 'id',
'terms' => $terms,
),
),
);
$query = new WP_Query( $args );
Andrea P comments:
then your naming is a bit uncommon, and that's maybe one reason why you struggle a bit with the urls and the archive names, even if you make a custom page for the "products" tax archive.
But the main reason for that struggle is because wordpress assumes that all first level urls ( like site.com/name/ ) are either single page/post/cpt or are a post_type archive. It's a convention on which all wp permalinks and redirects logic is based.
So, my suggestion is always to keep in mind these conventions ("limitations" if you want) when you build your cpts and taxonomies, and try to make your life as easy as possible, and think within the wp conventions, rather than fight with them. ;)
in any case, as Arnav has mentioned, you can specify different archive-names for the taxonomies and post types, so that they don't conflict one with another and so that you can create a custom-page which then take the place of what would be the url of a default post_type archive.
Sébastien | French WordpressDesigner comments:
I use a cpt "events". I rewrite the slug with "agenda".
I have a taxonomy "agenda".
When I open the post http://mysite.com/agenda/mypost/ I have a 404 error.
I refresh my permalink. I always have a 404.
I don't want to create an "agenda" page. It would appear in the backoffice when it is useless, it would not make sense.
Sébastien | French WordpressDesigner comments:
img = all settings in cpt_ui
Andrea P comments:
I am sorry but this is how wordpress works... and it works like that because of the core structure of the db and the permalinks/templates flow. It's not because they are lazy that they haven't implemented the automated taxonomy all-terms archive, it's because there isn't a way to easily handle it without conflicts.
Wp is a framework, and therefore it has to build permalink rewrites and templates-location based on rules which are built automatically from user-based structures of cpts and taxonomy and terms. If you build these rules bespoke for your site, and you can hardcode the specific slugs in the rewrite rules, then you can probably achieve your desired url structure.
But as wp rewrites rules are built with variables and wildcards, they can be flexible only up to a certain point, and it is very hard to ensure that every possible structure will work without conflicts.
In order to allow for a good extent of flexibility and the least possible conflicts, wp permalinks works in this way:
site.com/slug/ ---> anything first level
if a post type exists with archive named "slug", then it pulls the cpt archive
otherwise the default behaviour is to look for a post or page named "slug" (404 if doesn't exists)
site.com/slug/name/ --> anything second level
if a taxonomy named "slug" exists, pull the archive for the term "name" (404 if doesn't exist)
otherwise if a cpt named "slug" exists, then it pulls the single view and look for a post of type "slug" called "name" (404 the cpt "slug" doesn't exists, or if the post "name" doesn't exist)
otherwise assume that this is a page "name" with parent page "slug" (404 if doesn't exists)
and to make it even more complex, there are some other rules connected with these, to handle cases in which you have a base/prefix in the url of yout posts, or if you put the date/categories in the url etc...
I am not a wizard of rewrite rules and wildcards, but I can see the huge complexity of handling these rules with dynamic values for the taxonomy/terms/cpt slugs, and I feel that if after years this is the flow/priority we have, it's because it's the best for the most of the user-cases.
Unfortunately this doesn't exclude that in some cases this convention wouldn't work as you'd like to, but usually with a change in cpt/taxonomy structures or just some renaming, it's possible to find a decent compromise.
one thing that is often helpful is the fact that you can use a slug for the cpt-archive ( parameter "has_archive" => "slug-archive" ), and use a different one to prepend to the actual post url (parameter "rewrite" => ["slug" => "cpt-slug"] ). But probably for your "events" it won't help.
in your "events" case I think it would still conflict if you create a page "agenda" because wp would still assume is the cpt archive.
the real problem is the fact that you'd like to have the same base for the taxonomy and for the cpt. Even if wp had support for taxonomy all-terms archives, the permalinks will still conflict if you have a taxonomy with same slug as a cpt...
As I said before, it would be impossible for the auto-generated rules to guess if site.com/agenda/name/ is looking for the cpt with slug "agenda" or for the taxonomy with slug "agenda". There is no way to make it work in that way with dynamically built rules.
Usually in these cases I play around with singular/plurals slugs, so they kinda look the same and they kinda have the same meaning, but wp doesn't get confused because they are different.
by instance you could keep the rewrite as singular "agenda" for the events, because then the url site.com/agenda/event-name/ would be the singular event.
Then the url site.com/agenda/ will show the archive of all posts from the cpt "events".
and then put the taxonomy slug to be plural "agendas", so that the archives listing various events from the terms of the taxonomy "agendas" will have plural url like site.com/agendas/term-name/
And as a general suggestion, it is common convention to use singular slugs for the cpt names, so you would use "event" rather than "events", because as I said above, it makes more sense when the slug prefix is singular in the url of a single post. (though in your case you rewrite the slug anyway).
I hope this helps to clarify how WP handles these things, and maybe also provide a decent compromise to solve your problem.
cheers!
Sébastien | French WordpressDesigner comments:
Thank you for this long explanation.
We can create a mysite.com/clothes page and an archive for the term mysite.com/clothes/sunglasses
so I think we can create a cpt archive mysite.com/clothes and an archive for the term mysite.com/clothes/sunglasses
no ?
Sébastien | French WordpressDesigner comments:
and obviously if I create an archive for the "clothes" cpt, I will not create the "clothes" page in order to avoid conflicts.
Andrea P comments:
if you create a cpt with slug "clothes" then you cannot create a taxonomy with slug "clothes". WP will get confused with permalinks.
there is no way to avoid that wp gets confused.
you must have different slugs for your cpt and for the taxonomy.
then you won't even need a custom page because you will have:
site.com/cpt/ ----> default archive with all posts
site.com/cpt/name/ ----> single post
site.com/tax/name/ ----> list of posts from term "name"
Sébastien | French WordpressDesigner comments:
OK, thanks for all your explanations Andrea :)
I understood a lot of things
Arnav Joy answers:
Hi,
You can create custom post type archive file for that.
Make sure while registering post type .
has_archive is true.
You can read more about it here
https://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/
Sébastien | French WordpressDesigner comments:
the archive of the cpt "shop" have this url : http://mysite.com/shop
but I said about the url of the taxonomy "products".
There is no url for this taxo, isn't it ?
Sébastien | French WordpressDesigner comments:
arf... I understand what you say... the url of the cpt is http://mysite.com/shop but I can use another slug, like http://mysite.com/products. I can use the slug for the taxo and for the cpt, there is no conflict ?
But in this case I have no more the url for my cpt : http://mysite.com/shop
That's it ?
Arnav Joy comments:
You have to rewrite the slug of your post type..please check this
https://stackoverflow.com/questions/30610366/wordpress-custom-post-type-archive-url-and-default-query
Sébastien | French WordpressDesigner comments:
Hi Arnav,
I use a cpt "events". I rewrite the slug with "agenda".
I have a taxonomy "agenda".
When I open the post http://mysite.com/agenda/mypost/ I have a 404 error.
I refresh my permalink. I always have a 404.
Sébastien | French WordpressDesigner comments:
img = all settings in cpt_ui
Cesar Contreras answers:
take a look at the documentation of taxonomy templates, this is the link:
https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/