To get single or multiple terms belong to a taxonomy, you can use TermRepository
class. This brings to you 2 methods find(int $id)
and findAll()
.
Examples with find(int $id, string $taxonomy)
<?php
use Fantassin\Core\WordPress\Taxonomy\Repository\TermRepository;
$repository = new TermRepository();
$firstCategory = $repository->find( 1, 'category' );
In this example, I get term with ID 1 that belongs listed in Cateogry taxonomy. By default, find()
method return Term
entity.
Examples with findAll(array $options)
<?php
use Fantassin\Core\WordPress\Taxonomy\Repository\TermRepository;
$repository = new TermRepository();
$allCategories = $repository->findAll(['taxonomy' => 'category']);
Options argument follow the official documentation from get_terms method.
In this example, I get all terms from Category taxonomy, the return of findAll()
method is array of
.Term
entity
Term
Entity
You can get basic informations from term element with right typing :
getId()
: integergetSlug()
: stringgetName()
: stringgetDescription()
: stringgetParentId()
: integergetTaxonomy()
: string
In the same way you can fetch terms with strict typing.