Search.../

queryPosts( )

Creates a query to retrieve a list of posts.

Description

The queryPosts() function builds a query to retrieve a list of up to 100 posts, and returns a PostsQueryBuilder 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 PostsQueryBuilder functions onto the query. PostsQueryBuilder functions enable you to sort, filter, and control the results that queryPosts() returns.

queryPosts() runs with these PostsQueryBuilder defaults that can be overridden:

Note that the default limit is '50', but the max limit is '100'.

To learn how to query posts, refer to the table below.

The following PostsQueryBuilder functions are supported for the queryPosts() function. For a full description of the Posts object, see the object returned for the items property in PostsQueryResult.

PROPERTYSUPPORTED FILTERS & SORTING
_ideq(),ne(),hasSome(),ascending(),descending()
titleeq(),ne(),startsWith(),hasSome(),exists(),in(),ascending(),descending()
firstPublishedDateeq(),ne(),lt(),le(),gt(),ge(),in(),ascending(),descending()
lastPublishedDateeq(),ne(),lt(),le(),gt(),ge(),in(),ascending(),descending()
slugeq(),ne(),hasSome(),ascending(),descending()
featuredeq(),ne(),ascending(),descending()
pinnedeq(),ne(),ascending(),descending()
categoryIdshasSome(),hasAll()
coverMedia.enabledeq(),ne()
memberIdeq(),ne(),hasSome()
hashtagshasSome(),hasAll()
commentingEnabledeq(),ne(),ascending(),descending()
minutesToReadeq(),ne(),lt(),le(),gt(),ge(),in()
tagIdshasSome(),hasAll()
pricingPlanIdshasSome(),hasAll()
translationIdeq(),ne(),exists(),in()
languageeq(),ne(),hasSome(),exists(),in()
metrics.commentseq(),ne(),lt(),le(),gt(),ge(),in(),ascending(),descending()
metrics.likeseq(),ne(),lt(),le(),gt(),ge(),in(),ascending(),descending()
metrics.viewseq(),ne(),lt(),le(),gt(),ge(),in(),ascending(),descending()

Syntax

function queryPosts(options: QueryPostsOptions): PostsQueryBuilder

queryPosts Parameters

NAME
TYPE
DESCRIPTION
options
Optional
QueryPostsOptions

Options specifying which fields to return.

Returns

Return Type:

Was this helpful?

Retrieve a list of all posts

This example uses the queryPosts() function to retrieve a list of all posts.

Copy Code
1import { posts } from wix-blog-backend;
2
3export async function queryPostsFunction() {
4 try {
5 const results = await posts.queryPosts().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
20/* Returns:
21 * [
22 * {
23 * "_id": "0a265609-e16e-4c96-b71a-2a62b513e6b8",
24 * "categoryIds": [],
25 * "commentingEnabled": true,
26 * "excerpt": "some text",
27 * "featured": false,
28 * "firstPublishedDate": "2022-05-25T13:56:19.023Z",
29 * "hashtags": [],
30 * "language": "en",
31 * "lastPublishedDate": "2022-05-25T13:56:19.023Z",
32 * "media": {
33 * "wixMedia": {},
34 * "displayed": false,
35 * "custom": false
36 * },
37 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
38 * "minutesToRead": 1,
39 * "pinned": false,
40 * "pricingPlanIds": [],
41 * "relatedPostIds": [],
42 * "slug": "_new-post",
43 * "tagIds": [],
44 * "title": "new post",
45 * "translationId": ""
46 * },
47 * {
48 * "_id": "ec9d2c31-4dd8-4993-80d7-fbf8c3148492",
49 * "categoryIds": [],
50 * "commentingEnabled": true,
51 * "excerpt": "",
52 * "featured": false,
53 * "firstPublishedDate": "2022-04-27T07:10:09.020Z",
54 * "hashtags": [],
55 * "language": "en",
56 * "lastPublishedDate": "2022-04-27T07:10:09.020Z",
57 * "media": {
58 * "wixMedia": {
59 * "image": "wix:image://v1/11062b_049e75ff426d48d1a794debb05897c8c~mv2_d_4505_3003_s_4_2.jpg#originWidth=4505&originHeight=3003",
60 * },
61 * "displayed": true,
62 * "custom": false
63 * },
64 * "memberId": "117f476b-be6e-4849-83c0-908f5e0725e7",
65 * "minutesToRead": 0,
66 * "pinned": false,
67 * "preview": false,
68 * "pricingPlanIds": [],
69 * "relatedPostIds": [],
70 * "slug": "post-with-cover-image",
71 * "tagIds": [],
72 * "title": "post with cover image",
73 * "translationId": ""
74 * },
75 * {
76 * "_id": "6abf332c-353e-4074-8326-3c6350126bd4",
77 * "categoryIds": [],
78 * "commentingEnabled": true,
79 * "excerpt": "demo content",
80 * "featured": false,
81 * "firstPublishedDate": "2022-04-22T08:04:17.108Z",
82 * "hashtags": [],
83 * "language": "en",
84 * "lastPublishedDate": "2022-04-22T08:04:17.108Z",
85 * "media": {
86 * "wixMedia": {},
87 * "displayed": false,
88 * "custom": false
89 * },
90 * "memberId": "62607f07-dbb4-4022-bbfd-283f8422a072",
91 * "minutesToRead": 1,
92 * "pinned": false,
93 * "pricingPlanIds": [],
94 * "relatedPostIds": [],
95 * "slug": "another-impersonated-post",
96 * "tagIds": [] ,
97 * "title": "another impersonated post",
98 * "translationId": ""
99 * },
100 * {
101 * "_id": "2ad331d7-1308-4afd-b994-0768611b38df",
102 * "categoryIds": [],
103 * "commentingEnabled": true,
104 * "excerpt": "demo content",
105 * "firstPublishedDate": "2022-04-12T12:19:56.907Z",
106 * "featured": false,
107 * "hashtags": [],
108 * "language": "en",
109 * "lastPublishedDate": "2022-04-12T12:19:56.907Z",
110 * "media": {
111 * "wixMedia": {
112 * "image": "wix:image://v1/75059a_9f8cd2f1282c4dc7ae9a4bea155e2661~mv2.jpg#originWidth=602&originHeight=773"
113 * },
114 * "displayed": true,
115 * "custom": false
116 * },
117 * "memberId": "117f476b-be6e-4849-83c0-908f5e0725e7",
118 * "minutesToRead": 1,
119 * "pinned": false,
120 * "pricingPlanIds": [],
121 * "relatedPostIds": [],
122 * "slug": "demo-post",
123 * "tagIds": [],
124 * "title": "demo post",
125 * "translationId": ""
126 * },
127 * {
128 * "_id": "67bee986-3a54-4f08-bb37-a59152f31b75",
129 * "categoryIds": [],
130 * "commentingEnabled": true,
131 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
132 * "featured": false,
133 * "firstPublishedDate": "2022-03-09T13:15:14.566Z",
134 * "hashtags": [
135 * "vacation",
136 * "dream",
137 * "summer",
138 * "hashtag"
139 * ],
140 * "language": "en",
141 * "lastPublishedDate": "2022-05-03T10:26:13.781Z",
142 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
143 * "media": {
144 * "wixMedia": {
145 * "image": "wix:image://v1/75059a_cf2c39f511b6478eaea5b4f7713831c0~mv2.jpg#originWidth=1944&originHeight=1409",
146 * },
147 * "displayed": false,
148 * "custom": false
149 * },
150 * "minutesToRead": 2,
151 * "pinned": false,
152 * "pricingPlanIds": [],
153 * "relatedPostIds": [],
154 * "slug": "transform-your-winter-blues-into-winter-creativity",
155 * "tagIds": [],
156 * "title": "Transform your winter blues into winter creativity",
157 * "translationId": ""
158 * },
159 * {
160 * "_id": "dd0907fa-ca4b-4fe4-ab9e-625e4733691e",
161 * "categoryIds": [],
162 * "commentingEnabled": true,
163 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
164 * "featured": false,
165 * "firstPublishedDate": "2022-03-09T13:15:14.565Z",
166 * "hashtags": [
167 * "vacation",
168 * "dream",
169 * "summer",
170 * "hashtag"
171 * ],
172 * "language": "en",
173 * "lastPublishedDate": "2022-05-03T10:26:35.834Z",
174 * "media": {
175 * "wixMedia": {
176 * "image": "wix:image://v1/75059a_19d50c96541b4b1aa915d498b351bd17~mv2.jpg#originWidth=886&originHeight=1161",
177 * },
178 * "displayed": true,
179 * "custom": false
180 * },
181 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
182 * "minutesToRead": 2,
183 * "pinned": false,
184 * "pricingPlanIds": [],
185 * "relatedPostIds": [],
186 * "slug": "5-reasons-to-wake-up-at-5am",
187 * "title": "5 reasons to wake up at 5am",
188 * "tagIds": [],
189 * "translationId": ""
190 * },
191 * {
192 * "_id": "6a7c0e49-036b-4874-b4cd-e015a22e857c",
193 * "categoryIds": [],
194 * "commentingEnabled": true,
195 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
196 * "featured": false,
197 * "firstPublishedDate": "2022-03-09T13:15:14.564Z",
198 * "hashtags": [
199 * "vacation",
200 * "dream",
201 * "summer",
202 * "hashtag"
203 * ],
204 * "language": "en",
205 * "lastPublishedDate": "2022-05-03T10:26:57.855Z",
206 * "media": {
207 * "wixMedia": {
208 * "image": "wix:image://v1/75059a_9f8cd2f1282c4dc7ae9a4bea155e2661~mv2.jpg#originWidth=602&originHeight=773"
209 * },
210 * "displayed": true,
211 * "custom": false
212 * },
213 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
214 * "minutesToRead": 2,
215 * "relatedPostIds": [],
216 * "pinned": false,
217 * "pricingPlanIds": [],
218 * "slug": "how-decluttering-changed-my-life",
219 * "tagIds": [],
220 * "title": "How decluttering changed my life",
221 * "translationId": ""
222 * }
223 * ]
224 */
Query for posts with filters

This example uses the queryPosts() function to search for posts that have at least 1 of 2 specified hashtags, and less than 1 view.

Copy Code
1import { posts } from 'wix-blog-backend';
2
3/* Sample options values:
4 * {
5 * fieldsets: [
6 * 'URL'
7 * ]
8 * }
9 */
10
11export async function myQueryPostsFunction() {
12 try {
13 const result = await posts.queryPosts()
14 .hasSome('hashtags', ['sun','dream'])
15 .gt('metrics.views', 1)
16 .find();
17 console.log('Retrieved Result', result.items);
18 return result.items;
19 } catch (error) {
20 console.log(error);
21 }
22}
23
24
25/* Returns:
26 * [
27 * {
28 * "_id": "425a5dca-c32d-40e6-b2d7-a8ffa3addded",
29 * "categoryIds": [
30 * "1ea22fce-bc3c-4b78-9422-f0f367f8628e"
31 * ],
32 * "commentingEnabled": true,
33 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
34 * "title": "When expecting, expect the unexpected",
35 * "featured": false,
36 * "firstPublishedDate": "2022-03-09T13:18:14.562Z",
37 * "hashtags": [
38 * "vacation",
39 * "dream",
40 * "summer",
41 * "hashtag"
42 * ],
43 * "heroImage": "",
44 * "language": "en",
45 * "lastPublishedDate": "2022-06-06T13:23:04.650Z",
46 * "media": {
47 * "wixMedia": {
48 * "image": "wix:image://v1/75059a_9f8cd2f1282c4dc7ae9a4bea155e2661~mv2.jpg#originWidth=602&originHeight=773"
49 * },
50 * "displayed": true,
51 * "custom": false
52 * },
53 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
54 * "minutesToRead": 2,
55 * "moderationDetails": {},
56 * "pinned": false,
57 * "preview": false,
58 * "pricingPlanIds": [],
59 * "relatedPostIds": [],
60 * "slug": "when-expecting-expect-the-unexpected",
61 * "tagIds": [],
62 * "translationId": "",
63 * "url": "http://https://tadasz7.wixsite.com/blog-velo-events/post/my-when-expecting-expect-the-unexpected"
64 * },
65 * {
66 * "_id": "67bee986-3a54-4f08-bb37-a59152f31b75",
67 * "categoryIds": [],
68 * "commentingEnabled": true,
69 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
70 * "featured": false,
71 * "firstPublishedDate": "2022-03-09T13:15:14.566Z",
72 * "hashtags": [
73 * "vacation",
74 * "dream",
75 * "summer",
76 * "hashtag"
77 * ],
78 * "heroImage": "",
79 * "language": "en",
80 * "lastPublishedDate": "2022-05-03T10:26:13.781Z",
81 * "media": {
82 * "wixMedia": {
83 * "image": "wix:image://v1/75059a_cf2c39f511b6478eaea5b4f7713831c0~mv2.jpg#originWidth=1944&originHeight=1409",
84 * },
85 * "displayed": true,
86 * "custom": false
87 * },
88 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
89 * "minutesToRead": 2,
90 * "moderationDetails": {},
91 * "pinned": false,
92 * "pricingPlanIds": [],
93 * "relatedPostIds": [],
94 * "slug": "transform-your-winter-blues-into-winter-creativity",
95 * "tagIds": [],
96 * "title": "Transform your winter blues into winter creativity",
97 * "translationId": "",
98 * "url": "http://https://tadasz7.wixsite.com/blog-velo-events/post/transform-your-winter-blues-into-winter-creativity"
99 * },
100 * {
101 * "_id": "ccbb6257-ed0e-4521-97df-8b5b207adb00",
102 * "categoryIds": [
103 * "1ea22fce-bc3c-4b78-9422-f0f367f8628e"
104 * ],
105 * "commentingEnabled": true,
106 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
107 * "featured": true,
108 * "firstPublishedDate": "2020-08-05T21:00:00.000Z",
109 * "hashtags": [
110 * "sea",
111 * "sun"
112 * ],
113 * "heroImage": "",
114 * "language": "en",
115 * "lastPublishedDate": "2020-08-05T21:00:00.000Z",
116 * "media": {
117 * "wixMedia": {
118 * "image": "wix:image://v1/75059a_9f8cd2f1282c4dc7ae9a4bea155e2661~mv2.jpg#originWidth=602&originHeight=773"
119 * },
120 * "displayed": true,
121 * "custom": false
122 * },
123 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
124 * "minutesToRead": 1,
125 * "moderationDetails": {},
126 * "pinned": false,
127 * "pricingPlanIds": [
128 * "b6e94a0c-4d0f-435e-9602-0dd61d2aca37"
129 * ],
130 * "relatedPostIds": [
131 * "425a5dca-c32d-40e6-b2d7-a8ffa3addded"
132 * ],
133 * "slug": "my-vacation",
134 * "tagIds": [
135 * "b698f939-cab5-419b-9966-ba0fa3316de9"
136 * ],
137 * "title": "My vacation",
138 * "translationId": "3cd710b7-c28d-4547-9b8a-3c1ec776064b",
139 * "url": "http://https://tadasz7.wixsite.com/blog-velo-events/post/my-vacation"
140 * }
141 * ]
142 */