Design an intuitive term/taxonomy/category/tag interface
pkarl opened this issue · 5 comments
When I think about this problem, I ask myself: What is the simplest way to tell WordPress that I want a particular cross-section of content decorated with tags.
There are a few things that come to mind immediately:
- Categories are bullshit, so if anything, a
->category()
method will just be an alias to something else (similar to their role in WP) - Terms are unique more often than they belong to multiple taxonomies, so let's build for unique-ness but accommodate multiple term hits
- The term vs. taxonomy vs. tax_query nomenclature is the thinking that created this messy interface to begin with. I think if we can create a simple interface (like
->tag()
) that accepts a variety of inputs, we'll be better off. - this doesn't account for grabbing all posts within a taxonomy, I think that warrants a sep interface (
->taxonomy('some-tax-name')
)
So here's a first draft of a few simple scenarios:
<?php
// fetch all posts tagged with 'pineapple'
$qq->tag( 'pineapple' )->go();
// also works with term_id
$qq->tag( 42 )->go();
// fetch all posts tagged with 'pineapple' and 'cucumber'
$qq->tag( ['pineapple', 'cucumber'] )->go(); // defaults to AND
// fetch all posts tagged with 'pineapple' EXCEPT those tagged with 'cucumber'
$qq->tag( ['pineapple', '-cucumber'] )->go();
$qq->tag( [42, '-cucumber'] )->go();
$qq->tag( [42, -13] )->go();
// fetch all posts tagged with 'pineapple' OR 'cucmber'
$qq->tag( ['pineapple', 'cucumber'], 'OR' )->go();
// if you want to specify a taxonomy for some reason, you can
$qq->tag( ['sea_creatures'=>['chicken', 'tuna']] )->go();
There will be a challenge executing some of these with WP_Query (I'm not sure if the select this, but not this will work with tax_query
).
I guess just to be clear, here are the scenarios in order of (what I'm guessing) is most frequent -> least frequent:
- fetch all posts by a given tag
- fetch all posts that match multiple tags (with an AND or OR relationship)
- fetch all posts within a taxonomy
- fetch all posts from multiple tags that live in diff. taxonomies
- fill in results with posts tagged
sometag
up to N results - fetch all posts that match one tag while excluding another
@pkarl those scenarios make sense to me. I'm pretty down with the API with the exception of the tag
nomenclature. As you point out: category
is all smoke-and-mirrors. Just another taxonomy. The only modification I'd make is using term
in place of tag
.
The scenarios make sense to me. I've been mulling it over and can't really think of other circumstances.
@jarednova since it accepts a variety of inputs (categories, terms, taxonomy names) in various forms (arrays, strings, ints), I wanted to come up with a generic interface for all of these things. tag
doesn't feel exactly right, but it serves the purpose of avoiding a name that already exists in the messy WP nomenclature while still covering the conceptual bases.
I'd prefer to leave term
, category
and taxonomy
, etc. as aliases to this generic method.
When we see scenarios like $qq->term( ['fish'=>['bass', 'fluke'], ['pets'=>['cat', 'dog', 'fish']], 'AND' )->go()
I'd do a double take.
That said, I'm not married to the idea of a SINGLE interface for all this. I may be asking too much of a single method to carry the weight of WP's entire crazy taxonomy system. @jarednova on a scale of "one method, complex args" to "complex methods, simple args", what feels right for human-friendly WP taxonomy queries?
Based on an IRL conversation with @jarednova here's where things stand:
- There should be methods (even if they're aliases) for
tag
,category
,taxonomy
, andterm
- For complex taxonomy queries, we'll offer a method called
tax()
which will replacetag
in the examples above - tag-based exclusions are more important than I realized so moar tests!
Actually, I think this is as solid as the direction is going to get for alpha. Closing!