Skip to content

feat: add Timber::get_terms([..], ['merge' => false]);#3213

Open
Levdbas wants to merge 7 commits into2.xfrom
get_terms_merge
Open

feat: add Timber::get_terms([..], ['merge' => false]);#3213
Levdbas wants to merge 7 commits into2.xfrom
get_terms_merge

Conversation

@Levdbas
Copy link
Copy Markdown
Member

@Levdbas Levdbas commented Mar 16, 2026

Related:

Issue

When working on the PostQuery::terms() feature suggested by @cbirdsong , I ran into code code duplication between Post and PostQuery for splitting term results by taxonomy. Then I remembered the very old Feature request in #2211 and decided to give that a shot.

Solution

Moved the taxonomy splitting logic into TermFactory and exposed it through the existing but unused $options parameter in Timber::get_terms().

Changes:

  1. TermFactory Enhancement - Added from_with_options() method to handle the merge option and partition_tax_queries() helper. I am not really a fan of this. Maybe @gchtr or @nlemoine can lend some eyes on this?
  2. Timber API Update - Updated get_terms() to pass options through to TermFactory
  3. Post Simplification - Removed partition_tax_queries() and delegated to Timber::get_terms()

Impact

  • Centralizes term taxonomy splitting logic in one place (TermFactory)
  • Makes the feature discoverable through Timber::get_terms() public API
  • Maintains full backwards compatibility - existing code continues to work unchanged

(Backwards) Compatibility:
All existing Post::terms() and maybe future PostQuery::terms() calls work identically

Usage Changes

Users can now use the merge option directly with Timber::get_terms():

// Get terms grouped by taxonomy
$terms_by_tax = Timber::get_terms([
    'taxonomy' => ['category', 'post_tag'],
    'object_ids' => [123, 456],
], ['merge' => false]);

// Returns:
// [
//     'category' => [Term, Term, ...],
//     'post_tag' => [Term, Term, ...],
// ]

This was previously only available through Post::terms() and PostQuery::terms() methods, which continue to work as before:

// Still works exactly the same
$terms_by_tax = $post->terms(['category', 'post_tag'], ['merge' => false]);
$terms_by_tax = $posts->terms(['category', 'post_tag'], ['merge' => false]);

Considerations

Future enhancements:
The merge: false option name is functional but somewhat unintuitive (you set it to false to get grouping). Consider adding a group_by_taxonomy: true alias in the future for better developer experience

Testing

Unit tests included

Added two new tests in TermTest.php:

  • testGetTermsGroupedByTaxonomy() - Verifies merge: false correctly groups terms by taxonomy
  • testGetTermsMergedByDefault() - Verifies default merge: true behavior returns flat array

@Levdbas Levdbas requested review from gchtr and nlemoine as code owners March 16, 2026 20:04
@Levdbas Levdbas changed the title feat: add feat: add Timber::get_terms([..], ['merge' => false,]); Mar 16, 2026
@Levdbas Levdbas changed the title feat: add Timber::get_terms([..], ['merge' => false,]); feat: add Timber::get_terms([..], ['merge' => false,]); Mar 16, 2026
@Levdbas Levdbas changed the title feat: add Timber::get_terms([..], ['merge' => false,]); feat: add Timber::get_terms([..], ['merge' => false]); Mar 16, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 16, 2026

Codecov Report

❌ Patch coverage is 97.87234% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.03%. Comparing base (93e65ab) to head (5b738b6).
⚠️ Report is 6 commits behind head on 2.x.

Files with missing lines Patch % Lines
src/Factory/TermFactory.php 97.82% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                2.x    #3213      +/-   ##
============================================
+ Coverage     89.83%   90.03%   +0.19%     
- Complexity     1633     1643      +10     
============================================
  Files            57       57              
  Lines          5017     5027      +10     
============================================
+ Hits           4507     4526      +19     
+ Misses          510      501       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coveralls
Copy link
Copy Markdown

coveralls commented Mar 16, 2026

Coverage Status

coverage: 89.805% (+0.2%) from 89.606%
when pulling 5b738b6 on get_terms_merge
into 93e65ab on 2.x.

Copy link
Copy Markdown
Member

@gchtr gchtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Levdbas I saw that you updated the implementation and removed the from_with_options() method again in favor of an $options parameter in the from() method. This looks clean to me now.

About the following consideration:

Future enhancements:
The merge: false option name is functional but somewhat unintuitive (you set it to false to get grouping). Consider adding a group_by_taxonomy: true alias in the future for better developer experience

Now we have different terms for the merge parameter in various places: merge, partition, group, split. I think it would be better if we only used one term, and I like the suggestion of using group instead of merge. What this would mean:

  • Rename partition methods and comments to group in this PR
  • Leave the name of the $merge parameter, because that would be a breaking change
  • Starting using group in comments, variable names, methods, where we can.

I think this might help to understand this feature better.

…te documentation for term grouping by taxonomy
@Levdbas Levdbas requested a review from gchtr March 26, 2026 21:26
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.

3 participants