Creates a query to retrieve a list of posts.
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
.
function queryPosts(options: QueryPostsOptions): PostsQueryBuilder;
Options specifying which fields to return.
This example uses the queryPosts()
function to retrieve a list of all posts.
import { posts } from "wix-blog-backend";
export async function queryPostsFunction() {
try {
const results = await posts.queryPosts().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": "0a265609-e16e-4c96-b71a-2a62b513e6b8",
* "categoryIds": [],
* "commentingEnabled": true,
* "excerpt": "some text",
* "featured": false,
* "firstPublishedDate": "2022-05-25T13:56:19.023Z",
* "hashtags": [],
* "language": "en",
* "lastPublishedDate": "2022-05-25T13:56:19.023Z",
* "media": {
* "wixMedia": {},
* "displayed": false,
* "custom": false
* },
* "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
* "minutesToRead": 1,
* "pinned": false,
* "pricingPlanIds": [],
* "relatedPostIds": [],
* "slug": "_new-post",
* "tagIds": [],
* "title": "new post",
* "translationId": ""
* },
* {
* "_id": "ec9d2c31-4dd8-4993-80d7-fbf8c3148492",
* "categoryIds": [],
* "commentingEnabled": true,
* "excerpt": "",
* "featured": false,
* "firstPublishedDate": "2022-04-27T07:10:09.020Z",
* "hashtags": [],
* "language": "en",
* "lastPublishedDate": "2022-04-27T07:10:09.020Z",
* "media": {
* "wixMedia": {
* "image": "wix:image://v1/11062b_049e75ff426d48d1a794debb05897c8c~mv2_d_4505_3003_s_4_2.jpg#originWidth=4505&originHeight=3003",
* },
* "displayed": true,
* "custom": false
* },
* "memberId": "117f476b-be6e-4849-83c0-908f5e0725e7",
* "minutesToRead": 0,
* "pinned": false,
* "preview": false,
* "pricingPlanIds": [],
* "relatedPostIds": [],
* "slug": "post-with-cover-image",
* "tagIds": [],
* "title": "post with cover image",
* "translationId": ""
* },
* {
* "_id": "6abf332c-353e-4074-8326-3c6350126bd4",
* "categoryIds": [],
* "commentingEnabled": true,
* "excerpt": "demo content",
* "featured": false,
* "firstPublishedDate": "2022-04-22T08:04:17.108Z",
* "hashtags": [],
* "language": "en",
* "lastPublishedDate": "2022-04-22T08:04:17.108Z",
* "media": {
* "wixMedia": {},
* "displayed": false,
* "custom": false
* },
* "memberId": "62607f07-dbb4-4022-bbfd-283f8422a072",
* "minutesToRead": 1,
* "pinned": false,
* "pricingPlanIds": [],
* "relatedPostIds": [],
* "slug": "another-impersonated-post",
* "tagIds": [] ,
* "title": "another impersonated post",
* "translationId": ""
* },
* {
* "_id": "2ad331d7-1308-4afd-b994-0768611b38df",
* "categoryIds": [],
* "commentingEnabled": true,
* "excerpt": "demo content",
* "firstPublishedDate": "2022-04-12T12:19:56.907Z",
* "featured": false,
* "hashtags": [],
* "language": "en",
* "lastPublishedDate": "2022-04-12T12:19:56.907Z",
* "media": {
* "wixMedia": {
* "image": "wix:image://v1/75059a_9f8cd2f1282c4dc7ae9a4bea155e2661~mv2.jpg#originWidth=602&originHeight=773"
* },
* "displayed": true,
* "custom": false
* },
* "memberId": "117f476b-be6e-4849-83c0-908f5e0725e7",
* "minutesToRead": 1,
* "pinned": false,
* "pricingPlanIds": [],
* "relatedPostIds": [],
* "slug": "demo-post",
* "tagIds": [],
* "title": "demo post",
* "translationId": ""
* },
* {
* "_id": "67bee986-3a54-4f08-bb37-a59152f31b75",
* "categoryIds": [],
* "commentingEnabled": true,
* "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
* "featured": false,
* "firstPublishedDate": "2022-03-09T13:15:14.566Z",
* "hashtags": [
* "vacation",
* "dream",
* "summer",
* "hashtag"
* ],
* "language": "en",
* "lastPublishedDate": "2022-05-03T10:26:13.781Z",
* "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
* "media": {
* "wixMedia": {
* "image": "wix:image://v1/75059a_cf2c39f511b6478eaea5b4f7713831c0~mv2.jpg#originWidth=1944&originHeight=1409",
* },
* "displayed": false,
* "custom": false
* },
* "minutesToRead": 2,
* "pinned": false,
* "pricingPlanIds": [],
* "relatedPostIds": [],
* "slug": "transform-your-winter-blues-into-winter-creativity",
* "tagIds": [],
* "title": "Transform your winter blues into winter creativity",
* "translationId": ""
* },
* {
* "_id": "dd0907fa-ca4b-4fe4-ab9e-625e4733691e",
* "categoryIds": [],
* "commentingEnabled": true,
* "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
* "featured": false,
* "firstPublishedDate": "2022-03-09T13:15:14.565Z",
* "hashtags": [
* "vacation",
* "dream",
* "summer",
* "hashtag"
* ],
* "language": "en",
* "lastPublishedDate": "2022-05-03T10:26:35.834Z",
* "media": {
* "wixMedia": {
* "image": "wix:image://v1/75059a_19d50c96541b4b1aa915d498b351bd17~mv2.jpg#originWidth=886&originHeight=1161",
* },
* "displayed": true,
* "custom": false
* },
* "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
* "minutesToRead": 2,
* "pinned": false,
* "pricingPlanIds": [],
* "relatedPostIds": [],
* "slug": "5-reasons-to-wake-up-at-5am",
* "title": "5 reasons to wake up at 5am",
* "tagIds": [],
* "translationId": ""
* },
* {
* "_id": "6a7c0e49-036b-4874-b4cd-e015a22e857c",
* "categoryIds": [],
* "commentingEnabled": true,
* "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
* "featured": false,
* "firstPublishedDate": "2022-03-09T13:15:14.564Z",
* "hashtags": [
* "vacation",
* "dream",
* "summer",
* "hashtag"
* ],
* "language": "en",
* "lastPublishedDate": "2022-05-03T10:26:57.855Z",
* "media": {
* "wixMedia": {
* "image": "wix:image://v1/75059a_9f8cd2f1282c4dc7ae9a4bea155e2661~mv2.jpg#originWidth=602&originHeight=773"
* },
* "displayed": true,
* "custom": false
* },
* "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
* "minutesToRead": 2,
* "relatedPostIds": [],
* "pinned": false,
* "pricingPlanIds": [],
* "slug": "how-decluttering-changed-my-life",
* "tagIds": [],
* "title": "How decluttering changed my life",
* "translationId": ""
* }
* ]
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.