Skip to content

Update $post->terms() to allow parameters for the term query#1845

Merged
jarednova merged 9 commits intomasterfrom
post-terms
Nov 10, 2018
Merged

Update $post->terms() to allow parameters for the term query#1845
jarednova merged 9 commits intomasterfrom
post-terms

Conversation

@gchtr
Copy link
Copy Markdown
Member

@gchtr gchtr commented Nov 8, 2018

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():

$terms = $post->terms( array(
    'query' => array(
        'taxonomy' => 'custom_tax',
        'orderby'  => 'count',
    ),
    'merge'      => false,
    'term_class' => 'My_Term_Class'
) );

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 $merge and $TermClass parameters 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 merge parameter makes it trickier. Consider the query parameter for order. 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

  • use tag or tags instead of post_tag
  • use categories instead of category

I 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 named tag instead of post_tag. Also, what happens if there really is a custom taxonomy named tag?

I added some @todo tags for removing these in 2.x.

Testing

I added a couple of tests:

  • Duplicated existing tests for testing the new arguments array
  • Added tests for merge argument
  • Added tests for query arguments

@gchtr gchtr requested a review from pascalknecht November 8, 2018 21:55
@coveralls
Copy link
Copy Markdown

coveralls commented Nov 9, 2018

Coverage Status

Coverage increased (+0.03%) to 94.17% when pulling e27660e on post-terms into 987a900 on master.

@pascalknecht
Copy link
Copy Markdown
Contributor

Looks good @gchtr

@jarednova
Copy link
Copy Markdown
Member

@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
Copy link
Copy Markdown

codecov bot commented Nov 10, 2018

Codecov Report

Merging #1845 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ Complexity Δ
lib/Post.php 95.61% <100%> (+0.13%) 242 <19> (+4) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 987a900...e27660e. Read the comment docs.

@jarednova jarednova merged commit a2a6705 into master Nov 10, 2018
@jarednova
Copy link
Copy Markdown
Member

In regards to 2.x stuff — totally agree about post_tag vs. tag, etc. That was 2014-era Jared happily "fixing" WordPress. Seemed to make sense at the time, but the asking a bit more out of the dev is the right move here.

That said, please do not name your custom taxonomy "tag"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants