Skip to content

WordPress REST API Client for Java - Samples

Retrieving a Taxonomy

Retrieve information about a single WordPress taxonomy using the REST API.

This sample demonstrates how to:

  • Configure a WpRestClient using either Apache HttpClient or OkHttp.
  • Authenticate using either Basic Authentication or JWT Authentication.
  • Retrieve a taxonomy by its unique name.
  • Receive the resulting WpTaxonomyInfo instance returned by the WordPress REST API.
  • Access common taxonomy metadata such as the name, description, slug, supported object types, and whether the taxonomy is hierarchical.
  • Handle common error scenarios such as unauthorized access, insufficient permissions, or a non-existent taxonomy.
samples/src/main/java/io/github/evisentin/wordpress/rest/client/samples/code/taxonomies/GetTaxonomy.java
package io.github.evisentin.wordpress.rest.client.samples.code.taxonomies;

import io.github.evisentin.wordpress.rest.client.domain.WpRestClient;
import io.github.evisentin.wordpress.rest.client.domain.model.WpTaxonomyInfo;
import io.github.evisentin.wordpress.rest.client.samples.code.SampleClientBuilder;

import static io.github.evisentin.wordpress.rest.client.samples.code.SampleClientBuilder.AuthenticationType.BASIC_AUTH;
import static io.github.evisentin.wordpress.rest.client.samples.code.SampleClientBuilder.Implementation.APACHE;

/**
 * Demonstrates how to retrieve information about a single WordPress taxonomy.
 * <p>
 * This sample illustrates how to:
 * <ul>
 *     <li>Create a {@link WpRestClient}.</li>
 *     <li>Retrieve a taxonomy by its unique name.</li>
 *     <li>Access the returned {@link WpTaxonomyInfo} instance.</li>
 * </ul>
 * <p>
 * The returned {@link WpTaxonomyInfo} contains metadata describing the taxonomy, including its name, description,
 * slug, supported object types, and whether it is hierarchical.
 * <p>
 * Depending on the requested taxonomy and the current user's permissions, the client may throw exceptions such as:
 * <ul>
 *     <li>{@code WpForbiddenException} if access to the taxonomy is denied.</li>
 *     <li>{@code WpNotFoundException} if the taxonomy does not exist.</li>
 *     <li>{@code WpUnauthorizedException} if authentication is required or invalid.</li>
 * </ul>
 * <p>
 * Before running this sample, configure the connection details in {@link SampleClientBuilder} so they match your
 * WordPress installation.
 */
public class GetTaxonomy {
    public static void main(String[] args) {

        // Choose the desired HTTP client implementation and authentication mechanism.
        //
        // Supported combinations:
        //   buildClientFor(BASIC_AUTH, APACHE)
        //   buildClientFor(JWT,        APACHE)
        //   buildClientFor(BASIC_AUTH, OK_HTTP)
        //   buildClientFor(JWT,        OK_HTTP)
        final WpRestClient restClient = SampleClientBuilder.buildClientFor(BASIC_AUTH, APACHE);

        final WpTaxonomyInfo taxonomyInfo = restClient.taxonomies().get("post_tag");
        //taxonomyInfo.getName();
        //taxonomyInfo.getDescription();
        //taxonomyInfo.getSlug();
        //taxonomyInfo.getTypes();
        //taxonomyInfo.isHierarchical();

        // PLEASE NOTE: you might get
        // - WpForbiddenException
        // - WpNotFoundException
        // - WpUnauthorizedException
    }
}

Listing Taxonomies

Retrieve all available WordPress taxonomies using the REST API.

This sample demonstrates how to:

  • Configure a WpRestClient using either Apache HttpClient or OkHttp.
  • Authenticate using either Basic Authentication or JWT Authentication.
  • Retrieve all taxonomies available on the WordPress installation.
  • Receive the resulting map keyed by taxonomy name.
  • Access the WpTaxonomyInfo associated with each taxonomy to inspect its metadata.
samples/src/main/java/io/github/evisentin/wordpress/rest/client/samples/code/taxonomies/ListTaxonomies.java
package io.github.evisentin.wordpress.rest.client.samples.code.taxonomies;

import io.github.evisentin.wordpress.rest.client.domain.WpRestClient;
import io.github.evisentin.wordpress.rest.client.domain.model.WpTaxonomyInfo;
import io.github.evisentin.wordpress.rest.client.samples.code.SampleClientBuilder;

import java.util.Map;

import static io.github.evisentin.wordpress.rest.client.samples.code.SampleClientBuilder.AuthenticationType.BASIC_AUTH;
import static io.github.evisentin.wordpress.rest.client.samples.code.SampleClientBuilder.Implementation.APACHE;

/**
 * Demonstrates how to retrieve the available WordPress taxonomies.
 * <p>
 * This sample illustrates how to:
 * <ul>
 *     <li>Create a {@link WpRestClient}.</li>
 *     <li>Retrieve all taxonomies available on the WordPress installation.</li>
 *     <li>Access the returned {@link Map} keyed by taxonomy name.</li>
 *     <li>Inspect the {@link WpTaxonomyInfo} associated with each taxonomy.</li>
 * </ul>
 * <p>
 * The WordPress REST API returns the available taxonomies as a map whose keys are the taxonomy names (for example,
 * {@code category} and {@code post_tag}) and whose values contain the corresponding taxonomy metadata.
 * <p>
 * Before running this sample, configure the connection details in {@link SampleClientBuilder} so they match your
 * WordPress installation.
 */
public class ListTaxonomies {
    public static void main(String[] args) {

        // Choose the desired HTTP client implementation and authentication mechanism.
        //
        // Supported combinations:
        //   buildClientFor(BASIC_AUTH, APACHE)
        //   buildClientFor(JWT,        APACHE)
        //   buildClientFor(BASIC_AUTH, OK_HTTP)
        //   buildClientFor(JWT,        OK_HTTP)
        final WpRestClient restClient = SampleClientBuilder.buildClientFor(BASIC_AUTH, APACHE);

        // Returns a map, the key is the taxonomy's unique name.
        final Map<String, WpTaxonomyInfo> taxonomiesByName = restClient.taxonomies().list();
    }
}