Search.../

queryPostCountStats( )

Retrieves the number of published posts per month within a specified time range.

Description

The queryPostCountStats() function returns a Promise that resolves to the number of posts per month within the specified time range.

You can set the time range using the rangeStart and months properties. The time range always starts on the 1st day of the month set in rangeStart and includes the number of months following rangeStart. For example, if rangeStart is set to '2022-03-13' and months is set to 4, the time range will be from '2022-03-01' until '2022-06-30'. The time range ends on the last day of the month.

Note: If there are no published posts in a specific month, that month is not included in the response. For example, let's say a blog has 0 posts dated in February 2022. If rangeStart is set to '2022-01-01' and months is set to 3, the response includes postCount values for January and March, but not February.

Syntax

function queryPostCountStats(options: QueryPostCountStatsOptions): Promise<QueryPostCountStatsResponse>

queryPostCountStats Parameters

NAME
TYPE
DESCRIPTION
options
Optional
QueryPostCountStatsOptions

Options specifying time frame, sort, and filter.

Returns

Fulfilled - Post count stats.

Return Type:

Promise<
QueryPostCountStatsResponse
>
NAME
TYPE
DESCRIPTION
stats
Array<
PeriodPostCount
>

List of posts in specified order.

Was this helpful?

Get post count per month

This example uses queryPostCountStats() to retrieve the postCount of published posts per month.

Copy Code
1import { posts } from 'wix-blog-backend';
2
3export async function queryPostCountStatsFunction() {
4 try {
5 const result = await posts.queryPostCountStats();
6 const stats = result.stats;
7 const firstMonthPostCount = result.stats[0].postCount;
8 console.log('Retrieved Result:',result);
9 return result;
10 } catch (error) {
11 console.error(error);
12 };
13}
14
15/* Promise resolves to:
16 * {
17 * "stats": [
18 * {
19 * "periodStart": "2020-08-01T00:00:00.000Z",
20 * "postCount": 1
21 * },
22 * {
23 * "periodStart": "2021-03-01T00:00:00.000Z",
24 * "postCount": 2
25 * },
26 * {
27 * "periodStart": "2022-03-01T00:00:00.000Z",
28 * "postCount": 9
29 * },
30 * {
31 * "periodStart": "2022-04-01T00:00:00.000Z",
32 * "postCount": 6
33 * },
34 * {
35 * "periodStart": "2022-05-01T00:00:00.000Z",
36 * "postCount": 1
37 * },
38 * {
39 * "periodStart": "2022-06-01T00:00:00.000Z",
40 * "postCount": 2
41 * },
42 * {
43 * "periodStart": "2022-07-01T00:00:00.000Z",
44 * "postCount": 5
45 * }
46 * ]
47 * }
48 */
Get post count per month (export from backend code)

This example uses queryPostCountStats() to retrieve the postCount of published posts per month.

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { posts } from 'wix-blog-backend';
3
4export const queryPostCountStatsFunction = webMethod(Permissions.Anyone, async () => {
5 try {
6 const result = await posts.queryPostCountStats();
7 const stats = result.stats;
8 const firstMonthPostCount = result.stats[0].postCount;
9 console.log('Retrieved Result:',result);
10 return result;
11 } catch (error) {
12 console.error(error);
13 };
14});
15
16/* Promise resolves to:
17 * {
18 * "stats": [
19 * {
20 * "periodStart": "2020-08-01T00:00:00.000Z",
21 * "postCount": 1
22 * },
23 * {
24 * "periodStart": "2021-03-01T00:00:00.000Z",
25 * "postCount": 2
26 * },
27 * {
28 * "periodStart": "2022-03-01T00:00:00.000Z",
29 * "postCount": 9
30 * },
31 * {
32 * "periodStart": "2022-04-01T00:00:00.000Z",
33 * "postCount": 6
34 * },
35 * {
36 * "periodStart": "2022-05-01T00:00:00.000Z",
37 * "postCount": 1
38 * },
39 * {
40 * "periodStart": "2022-06-01T00:00:00.000Z",
41 * "postCount": 2
42 * },
43 * {
44 * "periodStart": "2022-07-01T00:00:00.000Z",
45 * "postCount": 5
46 * }
47 * ]
48 * }
49 */
50
Get post count per month within a given time period

This example uses queryPostCountStats() to retrieve the postCount of published posts per month within a given time frame.

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { posts } from 'wix-blog-backend';
3
4/* Sample options value:
5 * {
6 * rangeStart: new Date('2022-03-10'),
7 * months: 4
8 * }
9 */
10
11export const queryPostCountStatsFunction = webMethod(Permissions.Anyone, async (options) => {
12 try {
13 const result = await posts.queryPostCountStats(options);
14 const stats = result.stats;
15 const firstMonthPostCount = result.stats[0].postCount;
16 console.log('Retrieved Result:',result);
17 return result;
18 } catch (error) {
19 console.error(error);
20 };
21});
22
23
24/* Promise resolves to:
25 * {
26 * "stats": [
27 * {
28 * "periodStart": "2022-03-01T00:00:00.000Z",
29 * "postCount": 9
30 * },
31 * {
32 * "periodStart": "2022-04-01T00:00:00.000Z",
33 * "postCount": 6
34 * },
35 * {
36 * "periodStart": "2022-05-01T00:00:00.000Z",
37 * "postCount": 1
38 * },
39 * {
40 * "periodStart": "2022-06-01T00:00:00.000Z",
41 * "postCount": 2
42 * }
43 * ]
44 * }
45 */
46