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
Retrieve a list of all categories (export from backend code)

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

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