Creates a query to retrieve a list of categories.
The queryCategories()
function builds a query to retrieve a list of up to 100 categories per language, and returns a CategoriesQueryBuilder
object.
The returned object contains the query definition, which is typically used to run the query using the find()
function.
You can refine the query by chaining CategoriesQueryBuilder
functions to the query. CategoriesQueryBuilder
functions enable you to sort, filter, and control the results that queryCategories
returns. Any functions chained to the queryCategories()
function are applied in the order that they are called.
queryCategories()
runs with these CategoriesQueryBuilder
defaults, which you can override.
The following CategoriesQueryBuilder
functions are supported for queryCategories()
. For a full description of the Categories
object, see the object returned for the items
property in CategoriesQueryResult
.
PROPERTY | SUPPORTED FILTERS & SORTING |
---|---|
_id | eq() ,ne() ,hasSome() |
label | eq() ,ne() ,startsWith() ,hasSome() ,exists() ,in() ,ascending() ,descending() |
postCount | eq() ,ne() ,lt() ,le() ,gt() ,ge() ,in() ,ascending() ,descending() |
title | eq() ,ne() ,startsWith() ,hasSome() ,exists() ,in() ,ascending() ,descending() |
rank | eq() ,ne() ,lt() ,le() ,gt() ,ge() ,in() ,ascending() ,descending() |
displayPosition | eq() ,ne() ,lt() ,le() ,gt() ,ge() ,in() ,ascending() ,descending() |
translationId | eq() ,ne() ,exists() ,in() |
language | eq() ,ne() ,exists() ,in() ,ascending() ,descending() |
slug | hasSome() ,ascending() ,descending() |
function queryCategories(
options: QueryCategoriesOptions,
): CategoriesQueryBuilder;
Options specifying which fields to return.
This example uses the queryCategories()
function to retrieve a list of all categories.
import { categories } from "wix-blog-backend";
export async function queryCategoriesFunction() {
try {
const results = await categories.queryCategories().find();
const items = results.items;
const firstItem = items[0];
const pageSize = results.pageSize;
const hasNext = results.hasNext();
const hasPrev = results.hasPrev();
const length = results.length;
const query = results.query;
return items;
} catch (error) {
console.error(error);
}
}
/* Returns:
* [
* {
* "_id": "f489bf39-3297-4854-8429-e19dbefdca0e",
* "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
* "description": "my category description",
* "displayPosition": 0,
* "label": "My Category",
* "language": "en",
* "postCount": 1,
* "slug": "my-category",
* "title": "My Category",
* "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e"
* },
* {
* "_id": "686d5dd8-317a-4e3a-96b5-fe25f107aafd",
* "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
* "description": "my category description",
* "displayPosition": 1,
* "label": "My Category 2",
* "language": "lt",
* "postCount": 0,
* "slug": "my-category-2",
* "title": "My Category 2",
* "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e"
* },
* {
* "_id": "1ea22fce-bc3c-4b78-9422-f0f367f8628e",
* "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
* "description": "Posts about my summer",
* "displayPosition": 2,
* "label": "Summer",
* "language": "en",
* "postCount": 6,
* "slug": "summer-slug",
* "title": "Summer",
* "translationId": "973369ad-0d4b-41f5-a820-1eed7986e0de"
* },
* {
* "_id": "8bb208d0-bc3d-4caa-bbbf-775e52851d8f",
* "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
* "description": "Posts about my holidays",
* "displayPosition": 3,
* "label": "holidays",
* "language": "en",
* "postCount": 0,
* "slug": "holidays",
* "title": "Holidays",
* "translationId": ""
* }
* ]
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.