allow additional filtering on Timber\Post::terms() retrieval when calling wp…#1802
allow additional filtering on Timber\Post::terms() retrieval when calling wp…#1802mdahlke wants to merge 4 commits intotimber:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1802 +/- ##
=========================================
Coverage 94.59% 94.59%
Complexity 1532 1532
=========================================
Files 48 48
Lines 3606 3606
=========================================
Hits 3411 3411
Misses 195 195
Continue to review full report at Codecov.
|
|
@mdahlke Thank you for your first contribution 👍 . I had a quick look at the code. One thing is missing: Whenever a new feature gets added we also require unit tests with them to confirm the code actually works as intended. Would you be up to adding these tests or do you require assistance? |
|
@mdahlke Thanks for this pull request! I agree that we need this. Just today I ran into the same issue and had to solve it with a separate function. I’m very glad you raised this issue and provided a proposal for a solution 👍. It’s really great if we have so many developers who bring in their thoughts! 😎 But sometimes a little change like this can have a bigger impact than you initially thought or raise a bigger discussion. Adding another parameter to the function might seem like the easiest solution. But then we’d have one more optional parameter in that function. I’m always hesitant about adding too many optional parameters, because then some questions arise. How should we define which parameter is the most important? If somebody wants to change only the last parameter, does he always need to check the defaults for the other parameters and pass them as well? When using $terms = $post->terms( array(
'taxonomy' => 'custom_tax',
'orderby' => 'count',
) );I think it’s not important to know for a developer that in the back, Timber uses Of course this would be a breaking change and we’d have to merge it into the $terms = $post->terms( array(
'query' => [
'taxonomy' => 'custom_tax',
'orderby' => 'count',
],
'merge' => false,
'term_class' => 'My_Term_Class'
) );An arguments array allows you to only pass the parameters that you want to change. Remember, we just merged in a pattern like this in #1778 for $persons = new Timber\PostQuery( array(
'query' => array(
'post_type' => 'person',
),
'post_class' => 'Person',
) );Did I miss something? Any other opinions (@pascalknecht, @jarednova, @chrisvanpatten)? |
|
@gchtr I totally agree. I like the pattern of #1778 where we have ~3x arguments. This could be implemented here in a way that doesn't break backwards compatibility via an adapter function (not sure if that's what it's called, but basically function overloading) that will run a modified function when that first argument is a two-dimensional array. @mdahlke: is that something you'd be up for? |
|
@jarednova I think that @gchtr solution is a much better way of implementing this than my quick and dirty solution. I say go that direction with it instead of just arbitrarily adding another argument. |
|
@jarednova @mdahlke @pascalknecht Okay, I’m going to try to add this without breaking backwards compatibility 👍. |
…_get_post_terms
Issue
Getting the terms from a
Timber\Postobject usingTimber\Post::terms()can not be filtered by terms that do not have a parent (they are the top-most-level term)Solution
Add an additional argument to the method
Timber\Post::termsthat can be passed to the functionwp_get_post_termsthat is called within the methodImpact
Will have:
Usage Changes
Calling
Timber\Post::termsnow accepts a 4th argument that is an array of "Term query parameters", see WP_Term_Query::__construct() for supported arguments.