3

I've created a fairly basic form that allows users to filter post type archives by their taxonomies.

On custom post types this is super simple to do by simply setting ?taxonomy_name=term_slug in the URL. WP also supports an array of terms with ?taxonomy_name[]=term_slug1&taxonomy_name[]=term_slug2.

What's odd however is that on the archive for non custom post types (ie the "post" post type) this stops working. Setting ?category=term_slug does absolutely nothing.

How can I, preferably without using pre_get_posts or anything else more advanced, filter the regular posts by their taxonomies (category and post_tag)?

  • You got my hopes up :) But sadly no :( I would've found it extremely strange if that worked though, because the taxonomy is named "category". I wouldn't have been that surprised though seeing as this is WordPress after all :P – powerbuoy Jun 1 '16 at 11:49
  • Not sure I follow, but ?cat=category_slug doesn't work either. – powerbuoy Jun 1 '16 at 12:02
  • Aaah! Thank you :) That does work. How very annoying (and WordPress:y) though. This means I'll have to handle that particular taxonomy differently from all other taxonomies. Do you know if post_tag too only accepts ID:s and has a different name? – powerbuoy Jun 1 '16 at 12:10
  • Hehe ok, gaah sometimes WP drives you crazy :P Post your solution as an answer and I'll give you credit! – powerbuoy Jun 1 '16 at 12:18
  • Since I posted it as answer I am removing my comments :) – Sumit Jun 1 '16 at 12:28
0

Even if default posts are stored in same table but they have different behavior and different characteristics. CPT has post type archive but default post type archived is called as blog and the taxonomy filter is called as tag/category archive.

You can access the category/tag archive using

For categories:

/?cat={category_id} //e.g. ?/cat=5

For tags:

/?tag={tag_slug} //e.g. ?/tag=my_tag_slug

If you are willing to use pre_get_posts you can customize this in the way you want (That is what I love in WordPress <3) else stick with default way.

Another note: I recommend to leave the default posts for blog never use them for building something custom with those types instead use CPT as many as you want to expend your site from blog to CMS :)

| improve this answer | |
  • Annoyingly (because it's so inconsistent) enough this is correct :) So thanks. I have a follow-up question though. When going to the next page, and ending up on /blog/page/2/, setting ?cat={category_id} now gives a 404 :'( Any way to fix that? This applies to both the post post type and custom post types. – powerbuoy Jun 1 '16 at 15:45
  • You are welcome @powerbuoy Sorry to say but I am not able to reproduce this issue. Are you sure on page 2 posts exist for in that category? – Sumit Jun 1 '16 at 19:12
  • aha perhaps that's the issue. I'd prefer an empty "loop" though rather than a 404. – powerbuoy Jun 2 '16 at 9:00
  • I will say whenever a category or tag changed reset the pagination to 1, because who knows which category has how many pages :) – Sumit Jun 2 '16 at 13:29
  • That's true, but I would also like the user to be able to choose a category and then navigate to the next page... we'll see how I solve it. I guess I'd need to insert ?cat, ?tag etc into the pagination links. And hope I don't get a 404 :P – powerbuoy Jun 2 '16 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.