Update $post->terms() to allow parameters for the term query#1845
Update $post->terms() to allow parameters for the term query#1845
Conversation
More complicated queries should be written in PHP anyway
|
Looks good @gchtr |
|
@gchtr thanks for following-up with a comprehensive answer to #1802 (and @mdahlke for getting the ball rolling). Please ignore my review comment. I modified the tests to better cover what happens for exceptions. Turns out, as written before, it was covering-up a potential error-case. New tests in e27660e better cover. Looking good on my local, but I'm going to let Travis confirm ... |
Codecov Report
@@ Coverage Diff @@
## master #1845 +/- ##
============================================
+ Coverage 94.71% 94.74% +0.02%
- Complexity 1530 1534 +4
============================================
Files 48 48
Lines 3617 3632 +15
============================================
+ Hits 3426 3441 +15
Misses 191 191
Continue to review full report at Codecov.
|
|
In regards to That said, please do not name your custom taxonomy "tag" |
Ticket: #1802
Issue
It’s not yet possible to change the query parameters that are used for getting a post’s terms when using
$post->terms().Solution
This pull request adds support for more advanced arguments for
$post->terms():The new style only accepts one argument: An array of arguments. An arguments array allows you to only pass the parameters that you want to change. You don’t have to provide a value for all the parameters.
The old
$mergeand$TermClassparameters are being deprecated and only left there for backwards compatibility.It got a little more complicated than I thought. If we allow query parameters for getting the terms, then the
mergeparameter makes it trickier. Consider the query parameter fororder. If that is set, we can’t get the terms for each taxonomy separately (as it’s done now), because when they are merged after that loop, the order wouldn’t be right.I had to rewrite the function so that in the end
wp_get_post_terms()is only called once instead of being called once for all the taxonomies that are requested. This has an additional benefit that only one query has to be performed. Yay, better performance! 🙌For brevity in Twig, I think we should keep the possibility to chose a taxonomy by name only. I mean that it would be easier to write this
{{ post.terms('category')|join(', ') }}instead of writing this:
{{ post.terms({ query: { taxonomy: 'category', } })|join(', ') }}Anything that requires more parameters should probably be written in PHP and then passed to Twig.
Impact
Using query parameters becomes available. There’s no impact for backwards compatibility.
Considerations
The old parameters can be removed in version
2.x. I’ll add a separate pull request for this, once this is merged in.Currently, it’s possible to
tagortagsinstead ofpost_tagcategoriesinstead ofcategoryI would remove these checks. I think they are too forgiving. They might help, but they might also confuse: When you read
{% for post.terms('tag') %}, you might first think that there’s a custom taxonomy namedtaginstead ofpost_tag. Also, what happens if there really is a custom taxonomy namedtag?I added some
@todotags for removing these in2.x.Testing
I added a couple of tests:
mergeargument