Getting articles tagged with a specific taxonomy term

You can specify a taxonomy element in a content type. All content items of that type can be linked to specific taxonomy terms.

In this tutorial, you will learn how to use the Delivery API to retrieve only the content items tagged with a specific taxonomy term.

Table of contents

    1. Get taxonomy term codename

    First, you need to get the taxonomy term's codename.

    1. In Kentico Kontent, navigate to Content models .
    2. Click the Taxonomy tab.
    3. Select a taxonomy group.
    4. View a codename of a taxonomy term by clicking .

    After you copy the codename of the taxonomy term, you can build your request to the Delivery API.

    2. Get only articles with a specific taxonomy term

    The Taxonomy supporting element contains a list of the codenames of taxonomy terms that are linked to the given item.

    For example, you can use the elements.<taxonomy_element_codename>[contains] query parameter and a taxonomy term's codename to get only the content items linked with the given taxonomy term.

    Here's an example of retrieving all articles for a barista persona.

    • Java
    import com.kenticocloud.delivery_core.*; import com.kenticocloud.delivery_rx.*; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.functions.Function; // Prepares an array to hold strongly-typed models List<TypeResolver<?>> typeResolvers = new ArrayList<>(); // Registers the type resolver for articles typeResolvers.add(new TypeResolver<>(Article.TYPE, new Function<Void, Article>() { @Override public Article apply(Void input) { return new Article(); } })); // Prepares the DeliveryService configuration object String projectId = "975bf280-fd91-488c-994c-2f04416e5ee3"; IDeliveryConfig config = DeliveryConfig.newConfig(projectId) .withTypeResolvers(typeResolvers); // Initializes a DeliveryService for Java projects IDeliveryService deliveryService = new DeliveryService(config); // Gets articles tagged with specific taxonomy term using a simple request List<Article> items = deliveryService.<Article>items() .equalsFilter("system.type", "article") .containsFilter("elements.personas", Arrays.asList("barista")) .get() .getItems(); // Gets articles tagged with specific taxonomy term using RxJava2 deliveryService.<Article>items() .equalsFilter("system.type", "article") .containsFilter("elements.personas", Arrays.asList("barista")) .getObservable() .subscribe(new Observer<DeliveryItemListingResponse<Article>>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(DeliveryItemListingResponse<Article> response) { // Gets the articles List<Article> items = response.getItems(); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } });
    import com.kenticocloud.delivery_core.*; import com.kenticocloud.delivery_rx.*; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.functions.Function; // Prepares an array to hold strongly-typed models List<TypeResolver<?>> typeResolvers = new ArrayList<>(); // Registers the type resolver for articles typeResolvers.add(new TypeResolver<>(Article.TYPE, new Function<Void, Article>() { @Override public Article apply(Void input) { return new Article(); } })); // Prepares the DeliveryService configuration object String projectId = "975bf280-fd91-488c-994c-2f04416e5ee3"; IDeliveryConfig config = DeliveryConfig.newConfig(projectId) .withTypeResolvers(typeResolvers); // Initializes a DeliveryService for Java projects IDeliveryService deliveryService = new DeliveryService(config); // Gets articles tagged with specific taxonomy term using a simple request List<Article> items = deliveryService.<Article>items() .equalsFilter("system.type", "article") .containsFilter("elements.personas", Arrays.asList("barista")) .get() .getItems(); // Gets articles tagged with specific taxonomy term using RxJava2 deliveryService.<Article>items() .equalsFilter("system.type", "article") .containsFilter("elements.personas", Arrays.asList("barista")) .getObservable() .subscribe(new Observer<DeliveryItemListingResponse<Article>>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(DeliveryItemListingResponse<Article> response) { // Gets the articles List<Article> items = response.getItems(); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } });
    • Swift
    import KenticoCloud let client = DeliveryClient.init(projectId: "975bf280-fd91-488c-994c-2f04416e5ee3") let customQuery = "items?system.type=article&elements.personas=barista" // More about strongly-typed models https://github.com/Kentico/cloud-sdk-swift#using-strongly-typed-models client.getItems(modelType: Article.self, customQuery: customQuery) { (isSuccess, itemsResponse, error) in if isSuccess { if let articles = itemsResponse?.items { // Use your items here } } else { if let error = error { print(error) } } }
    import KenticoCloud let client = DeliveryClient.init(projectId: "975bf280-fd91-488c-994c-2f04416e5ee3") let customQuery = "items?system.type=article&elements.personas=barista" // More about strongly-typed models https://github.com/Kentico/cloud-sdk-swift#using-strongly-typed-models client.getItems(modelType: Article.self, customQuery: customQuery) { (isSuccess, itemsResponse, error) in if isSuccess { if let articles = itemsResponse?.items { // Use your items here } } else { if let error = error { print(error) } } }
    • Java
    import com.kenticocloud.delivery; DeliveryClient client = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3"); List<NameValuePair> params = DeliveryParameterBuilder.params() .filterEquals("system.type", "article") .filterContains("elements.personas", "barista") .build(); // Create strongly typed models according to https://docs.kontent.ai/strongly-typed-models List<ArticleItem> items = client.getItems(ArticleItem.class, params);
    import com.kenticocloud.delivery; DeliveryClient client = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3"); List<NameValuePair> params = DeliveryParameterBuilder.params() .filterEquals("system.type", "article") .filterContains("elements.personas", "barista") .build(); // Create strongly typed models according to https://docs.kontent.ai/strongly-typed-models List<ArticleItem> items = client.getItems(ArticleItem.class, params);
    • JavaScript
    const KontentDelivery = require("@kentico/kontent-delivery"); // Create strongly typed models according to https://docs.kontent.ai/strongly-typed-models class Article extends KontentDelivery.ContentItem { constructor() { super(); } } const deliveryClient = new KontentDelivery.DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3", typeResolvers: [ new KontentDelivery.TypeResolver("article", (rawData) => new Article()) ] }); deliveryClient.items() .type("article") .containsFilter("elements.personas", ["barista"]) .toObservable() .subscribe(response => console.log(response));
    const KontentDelivery = require("@kentico/kontent-delivery"); // Create strongly typed models according to https://docs.kontent.ai/strongly-typed-models class Article extends KontentDelivery.ContentItem { constructor() { super(); } } const deliveryClient = new KontentDelivery.DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3", typeResolvers: [ new KontentDelivery.TypeResolver("article", (rawData) => new Article()) ] }); deliveryClient.items() .type("article") .containsFilter("elements.personas", ["barista"]) .toObservable() .subscribe(response => console.log(response));
    • C#
    using Kentico.Kontent.Delivery; // Initializes a content delivery client IDeliveryClient client = DeliveryClientBuilder .WithProjectId("975bf280-fd91-488c-994c-2f04416e5ee3") .Build(); // Gets articles tagged with the barista persona // Create strongly typed models according to https://docs.kontent.ai/strongly-typed-models DeliveryItemListingResponse<Article> response = await client.GetItemsAsync<Article>( new EqualsFilter("system.type", "article"), new ContainsFilter("elements.personas", "barista") ); var items = response.Items;
    using Kentico.Kontent.Delivery; // Initializes a content delivery client IDeliveryClient client = DeliveryClientBuilder .WithProjectId("975bf280-fd91-488c-994c-2f04416e5ee3") .Build(); // Gets articles tagged with the barista persona // Create strongly typed models according to https://docs.kontent.ai/strongly-typed-models DeliveryItemListingResponse<Article> response = await client.GetItemsAsync<Article>( new EqualsFilter("system.type", "article"), new ContainsFilter("elements.personas", "barista") ); var items = response.Items;
    • PHP
    <?php // Defined by Composer to include required libraries require __DIR__ . "/vendor/autoload.php"; use Kentico\Kontent\Delivery\DeliveryClient; use Kentico\Kontent\Delivery\QueryParams; $client = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3"); $items = $client->getItems((new QueryParams()) ->equals("system.type", "article") ->contains("personas", "barista"));
    <?php // Defined by Composer to include required libraries require __DIR__ . "/vendor/autoload.php"; use Kentico\Kontent\Delivery\DeliveryClient; use Kentico\Kontent\Delivery\QueryParams; $client = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3"); $items = $client->getItems((new QueryParams()) ->equals("system.type", "article") ->contains("personas", "barista"));
    • cURL
    curl --request GET \ --url "https://deliver.kontent.ai/975bf280-fd91-488c-994c-2f04416e5ee3/items?system.type=article&elements.personas[contains]=barista" --header "content-type: application/json"
    curl --request GET \ --url "https://deliver.kontent.ai/975bf280-fd91-488c-994c-2f04416e5ee3/items?system.type=article&elements.personas[contains]=barista" --header "content-type: application/json"
    • Ruby
    require "delivery-sdk-ruby" delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: "975bf280-fd91-488c-994c-2f04416e5ee3" delivery_client.items([ "system.type".eq("article"), "elements.personas".contains("barista") ]) .execute do |response| items = response.items items.each { |item| puts item.system.codename } end
    require "delivery-sdk-ruby" delivery_client = Kentico::Kontent::Delivery::DeliveryClient.new project_id: "975bf280-fd91-488c-994c-2f04416e5ee3" delivery_client.items([ "system.type".eq("article"), "elements.personas".contains("barista") ]) .execute do |response| items = response.items items.each { |item| puts item.system.codename } end
    • TypeScript
    import { ContentItem, DeliveryClient, Elements, TypeResolver } from "@kentico/kontent-delivery"; // Create strongly typed models according to https://docs.kontent.ai/strongly-typed-models export class Article extends ContentItem { public title: Elements.TextElement; public summary: Elements.TextElement; public post_date: Elements.DateTimeElement; public teaser_image: Elements.AssetsElement; public related_articles: Article[]; } const deliveryClient = new DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3", typeResolvers: [ new TypeResolver("article", (rawData) => new Article) ] }); deliveryClient.items<Article>() .type("article") .containsFilter("elements.personas", ["barista"]) .toObservable() .subscribe(response => console.log(response));
    import { ContentItem, DeliveryClient, Elements, TypeResolver } from "@kentico/kontent-delivery"; // Create strongly typed models according to https://docs.kontent.ai/strongly-typed-models export class Article extends ContentItem { public title: Elements.TextElement; public summary: Elements.TextElement; public post_date: Elements.DateTimeElement; public teaser_image: Elements.AssetsElement; public related_articles: Article[]; } const deliveryClient = new DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3", typeResolvers: [ new TypeResolver("article", (rawData) => new Article) ] }); deliveryClient.items<Article>() .type("article") .containsFilter("elements.personas", ["barista"]) .toObservable() .subscribe(response => console.log(response));