Search.../

queryCategories( )

Creates a query to retrieve a list of categories.

Description

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.

PROPERTYSUPPORTED FILTERS & SORTING
_ideq(),ne(),hasSome()
labeleq(),ne(),startsWith(),hasSome(),exists(),in(),ascending(),descending()
postCounteq(),ne(),lt(),le(),gt(),ge(),in(),ascending(),descending()
titleeq(),ne(),startsWith(),hasSome(),exists(),in(),ascending(),descending()
rankeq(),ne(),lt(),le(),gt(),ge(),in(),ascending(),descending()
displayPositioneq(),ne(),lt(),le(),gt(),ge(),in(),ascending(),descending()
translationIdeq(),ne(),exists(),in()
languageeq(),ne(),exists(),in(),ascending(),descending()
slughasSome(),ascending(),descending()

Syntax

function queryCategories(options: QueryCategoriesOptions): CategoriesQueryBuilder

queryCategories Parameters

NAME
TYPE
DESCRIPTION
options
Optional
QueryCategoriesOptions

Options specifying which fields to return.

Returns

Was this helpful?

Retrieve a list of all categories

This example uses the queryCategories() function to retrieve a list of all categories.

Copy Code
1import { categories } from 'wix-blog-backend';
2
3export async function queryCategoriesFunction() {
4 try {
5 const results = await categories.queryCategories().find();
6 const items = results.items;
7 const firstItem = items[0];
8 const pageSize = results.pageSize;
9 const hasNext = results.hasNext();
10 const hasPrev = results.hasPrev();
11 const length = results.length;
12 const query = results.query;
13 return items;
14 } catch (error) {
15 console.error(error);
16 }
17}
18
19/* Returns:
20 * [
21 * {
22 * "_id": "f489bf39-3297-4854-8429-e19dbefdca0e",
23 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
24 * "description": "my category description",
25 * "displayPosition": 0,
26 * "label": "My Category",
27 * "language": "en",
28 * "postCount": 1,
29 * "slug": "my-category",
30 * "title": "My Category",
31 * "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e"
32 * },
33 * {
34 * "_id": "686d5dd8-317a-4e3a-96b5-fe25f107aafd",
35 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
36 * "description": "my category description",
37 * "displayPosition": 1,
38 * "label": "My Category 2",
39 * "language": "lt",
40 * "postCount": 0,
41 * "slug": "my-category-2",
42 * "title": "My Category 2",
43 * "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e"
44 * },
45 * {
46 * "_id": "1ea22fce-bc3c-4b78-9422-f0f367f8628e",
47 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
48 * "description": "Posts about my summer",
49 * "displayPosition": 2,
50 * "label": "Summer",
51 * "language": "en",
52 * "postCount": 6,
53 * "slug": "summer-slug",
54 * "title": "Summer",
55 * "translationId": "973369ad-0d4b-41f5-a820-1eed7986e0de"
56 * },
57 * {
58 * "_id": "8bb208d0-bc3d-4caa-bbbf-775e52851d8f",
59 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
60 * "description": "Posts about my holidays",
61 * "displayPosition": 3,
62 * "label": "holidays",
63 * "language": "en",
64 * "postCount": 0,
65 * "slug": "holidays",
66 * "title": "Holidays",
67 * "translationId": ""
68 * }
69 * ]
70 */
71
Query for a category with filters

This example uses the queryCategories() function to search for categories in descending order that have a displayPosition less than or equal to 2.

Copy Code
1import { categories } from 'wix-blog-backend';
2
3/* Sample options values:
4 * {
5 * fieldsets: [
6 * 'URL'
7 * ]
8 * }
9 */
10
11export async function queryCategoriesFunction() {
12 try {
13 const result = await categories.queryCategories({fieldsets: ['URL']})
14 .descending('label')
15 .lt('displayPosition', 3)
16 .find();
17 console.log('Retrieved result:', result.items);
18 return result.items;
19 } catch (error) {
20 console.log(error);
21 }
22}
23
24/* Returns:
25 * [
26 * {
27 * "_id": "1ea22fce-bc3c-4b78-9422-f0f367f8628e",
28 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245",
29 * "description": "Posts about my summer",
30 * "displayPosition": 2,
31 * "label": "Summer",
32 * "language": "en",
33 * "postCount": 6,
34 * "slug": "summer-slug"
35 * "title": "Summer",
36 * "translationId": "973369ad-0d4b-41f5-a820-1eed7986e0de",
37 * "url": "http://https://tadasz7.wixsite.com/blog-velo-events/my-blog/categories/summer-slug"
38 * },
39 * {
40 * "_id": "f489bf39-3297-4854-8429-e19dbefdca0e",
41 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245"
42 * "description": "my category description",
43 * "displayPosition": 0,
44 * "label": "My Category",
45 * "language": "en",
46 * "postCount": 1,
47 * "slug": "my-category",
48 * "title": "My Category",
49 * "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e",
50 * "url": "http://https://tadasz7.wixsite.com/blog-velo-events/my-blog/categories/my-category"
51 * },
52 * {
53 * "_id": "686d5dd8-317a-4e3a-96b5-fe25f107aafd",
54 * "coverImage": "wix:image://v1/162e66_f6bffd1cd6144ddf87325b82fe8f42ed~mv2.jpg#originWidth=385&originHeight=245"
55 * "description": "my category description",
56 * "displayPosition": 1,
57 * "label": "My Category 2",
58 * "language": "lt",
59 * "postCount": 0,
60 * "slug": "my-category-2",
61 * "title": "My Category 2",
62 * "translationId": "dfc5b1a7-df04-4596-b311-9724f0477c3e",
63 * "url": "http://https://tadasz7.wixsite.com/blog-velo-events/my-blog/categories/my-category-2"
64 * }
65 * ]
66 */
67