Working with Taxonomies

Creating and querying custom taxonomies is easy with better-wordpress.

Creating

As shown in the configuration guide, registering taxonomies alongside your custom post types is very straightforward. Simply add the taxonomies key to your post type object with a list of slugs to be registered:

config.php
"custom-post-types" => [
    [
        "slug" => "album",
        "icon" => "dashicons-album",
        "taxonomies" => ['genre'],
    ],
],
Registering a custom taxonomy.

And just like that, we have a new taxonomy on our admin page:

The display name will automatically be set as a capitalized version of the slug.

Querying

Getting all the terms

Similar to the way posts are queried in better-wordpress, you can declare the specific attributes of each term that will be returned.

For example if you want to grab the name, slug, and url for each term in the ‘genre’ taxonomy:

$genres = site()->getTerm('genre', ['name','slug','url']);
Getting the name, slug, and url for each term in the "genre" taxonomy.

Which would result in $genres looking like this:

[
    [
        "name" => "Synth Pop",
        "slug" => "synth_pop",
        "url" => "https://my.music.site/genre/synth_pop"
    ],
    [
        "name" => "Jazz",
        "slug" => "jazz",
        "url" => "https://my.music.site/genre/jazz"
    ],
    [
        "name" => "Electronica",
        "slug" => "electronica",
        "url" => "https://my.music.site/genre/electronica"
    ],   
]

See more on the getTerm page.

Getting the terms of a post

Using the getPostTaxonomy function we can easily find out which terms of a given taxonomy a post has. Simply supply a WP_Post and a taxonomy slug:

$post_genres = site()->getPostTaxonomy(get_post(), 'genre');
Get all the "genre" terms of the current post.

And just like that, $post_genres is a list of WP_Term objects:

[
    object(WP_Term) [
        "term_id" => 1,
        "name" => "Synth Pop",
        "slug" => "synth_pop",
        ... // Other WP_Term attributes
    ],
    object(WP_Term) [
        "term_id" => 3,
        "name" => "Electronica",
        "slug" => "electronica",
        ...
    ],
]

See more on the getPostTaxonomy page.

Querying posts by terms

Another common use of taxonomies is grabbing every post that has a certain term. With better-wordpress this has never been easier.

As seen in the Site Object Guide, the key is adding taxonomy.<slug> to the query:

$jazz_albums = site()->getPosts([
  "post_type" => "album",
  "taxonomy.genre" => "jazz"
  "numberposts" => -1, // Get every post
]);
Get every album post where the genre is "jazz".

Since we only set the first parameter of getPosts, $jazz_albums will simply be an array of WP_Post objects. To see an example with output fields defined, check out this “Putting it Together” section.

Additionally if you want to grab posts for multiple terms, just comma-seperate your string:

$jazz_and_electronica_albums = site()->getPosts([
  "post_type" => "album",
  "taxonomy.genre" => "jazz,electronica"
  "numberposts" => -1, // Get every post
]);
Get every album post where the genre is either "jazz" or "electronica".

More about querying posts: