kaiseki / wp-get-terms-by-post-type
Extend WordPress get_terms() to count and constrain terms by the post types attached to them
Package info
github.com/kaisekidev/kaiseki-wp-get-terms-by-post-type
pkg:composer/kaiseki/wp-get-terms-by-post-type
Requires
- php: ^8.2
- kaiseki/config: ^2.0
- kaiseki/wp-hook: ^2.0
- psr/container: ^1.1 || ^2.0
Requires (Dev)
- bnf/phpstan-psr-container: ^1.1
- kaiseki/php-coding-standard: ^1.0
- maglnet/composer-require-checker: ^4.0
- php-stubs/wordpress-stubs: ^6.2
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^11.0
- roave/security-advisories: dev-latest
- roots/wordpress-core-installer: *
- roots/wordpress-no-content: @stable
- szepeviktor/phpstan-wordpress: ^2.0
- thecodingmachine/phpstan-safe-rule: ^1.4
This package is auto-updated.
Last update: 2026-06-02 23:45:38 UTC
README
Extend WordPress get_terms() to count and constrain terms by the post types attached to them.
A single kaiseki/wp-hook HookProviderInterface (HookRegistry) that filters terms_clauses so a
get_terms() query restricted with a post_type argument returns only terms attached to those post
types, each with a count of matching posts. By default only published posts are counted; pass
post_status to override.
Installation
composer require kaiseki/wp-get-terms-by-post-type
Requires PHP 8.2 or newer.
Usage
Register ConfigProvider with your laminas-style config aggregator and activate the provider via
kaiseki/wp-hook:
use Kaiseki\WordPress\GetTermsByPostType\HookRegistry; return [ 'hook' => [ 'provider' => [ HookRegistry::class, ], ], ];
With the provider active, pass post_type (and optionally post_status) to get_terms():
$terms = get_terms([ 'taxonomy' => 'category', 'post_type' => ['post', 'page'], // string or list of strings 'post_status' => ['publish'], // optional; defaults to ['publish'] 'hide_empty' => true, ]); // Each returned term now carries a `count` of posts in the given post types.
Queries that request 'fields' => 'count' are passed through unchanged.
Development
composer install
composer check # check-deps, cs-check, phpstan
License
MIT — see LICENSE.