Search.../

getPostBySlug( )

Gets a post by the provided slug.

Description

The getPostBySlug() function returns a Promise that resolves to a post whose slug matches the given slug.

The slug is the end of a post's URL that refers to a specific post. For example, if a post's URL is https:/example.com/blog/post/my-post-slug, the slug is my-post-slug. The slug is case-sensitive, and is generally derived from the post title, unless specified otherwise.

Syntax

function getPostBySlug(slug: string, options: GetPostBySlugOptions): Promise<GetPostBySlugResponse>

getPostBySlug Parameters

NAME
TYPE
DESCRIPTION
slug
string

Slug of the post to retrieve.

The end of a post's URL, for example, https:/example.com/blog/post/my-post-slug. Case sensitive and generally based on the post title if not specified.

options
Optional
GetPostBySlugOptions

Options specifying which fields to return.

Returns

Fulfilled - The requested post.

Return Type:

Promise<
GetPostBySlugResponse
>
NAME
TYPE
DESCRIPTION
post
Post

Post info.

Was this helpful?

Get a post by its slug

Copy Code
1import { posts } from 'wix-blog-backend';
2
3/* Sample slug value:
4 * 'my-slug'
5 */
6
7export async function getPostBySlugFunction(slug) {
8
9 try {
10 const result = await posts.getPostBySlug(slug);
11 const title = result.post.title;
12 const postId = result.post._id;
13 console.log('Retrieved Result:', result);
14 return result;
15 } catch (error) {
16 console.error(error);
17 }
18}
19
20
21/* Promise resolves to:
22 * {
23 * "post": {
24 * "_id": "ccbb6257-ed0e-4521-97df-8b5b207adb00",
25 * "categoryIds": [
26 * "1ea22fce-bc3c-4b78-9422-f0f367f8628e"
27 * ],
28 * "commentingEnabled": true,
29 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
30 * "featured": true,
31 * "firstPublishedDate": "2020-08-05T21:00:00.000Z",
32 * "hashtags": [
33 * "sea",
34 * "sun"
35 * ],
36 * "heroImage": ""
37 * "language": "en",
38 * "lastPublishedDate": "2020-08-05T21:00:00.000Z",
39 * "media": {
40 * "wixMedia": {
41 * "image": "wix:image://v1/75059a_9f8cd2f1282c4dc7ae9a4bea155e2661~mv2.jpg#originWidth=602&originHeight=773"
42 * },
43 * "displayed": true,
44 * "custom": false
45 * },
46 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
47 * "minutesToRead": 1,
48 * "moderationDetails": {},
49 * "pinned": false,
50 * "preview": false,
51 * "pricingPlanIds": [
52 * "b6e94a0c-4d0f-435e-9602-0dd61d2aca37"
53 * ],
54 * "relatedPostIds": [
55 * "425a5dca-c32d-40e6-b2d7-a8ffa3addded"
56 * ],
57 * "slug": "my-vacation",
58 * "tagIds": [
59 * "b698f939-cab5-419b-9966-ba0fa3316de9"
60 * ],
61 * "title": "My vacation",
62 * "translationId": "3cd710b7-c28d-4547-9b8a-3c1ec776064b"
63 * }
64 * }
65 */
Get a post by its slug (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { posts } from 'wix-blog-backend';
3
4/* Sample slug value:
5 * 'my-slug'
6 */
7
8export const getPostBySlugFunction = webMethod(Permissions.Anyone, async (slug) => {
9 try {
10 const result = await posts.getPostBySlug(slug);
11 const title = result.post.title;
12 const postId = result.post._id;
13 console.log('Retrieved Result:', result);
14 return result;
15 } catch (error) {
16 console.error(error);
17 }
18});
19
20/* Promise resolves to:
21 * {
22 * "post": {
23 * "_id": "ccbb6257-ed0e-4521-97df-8b5b207adb00",
24 * "categoryIds": [
25 * "1ea22fce-bc3c-4b78-9422-f0f367f8628e"
26 * ],
27 * "commentingEnabled": true,
28 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
29 * "featured": true,
30 * "firstPublishedDate": "2020-08-05T21:00:00.000Z",
31 * "hashtags": [
32 * "sea",
33 * "sun"
34 * ],
35 * "heroImage": ""
36 * "language": "en",
37 * "lastPublishedDate": "2020-08-05T21:00:00.000Z",
38 * "media": {
39 * "wixMedia": {
40 * "image": "wix:image://v1/75059a_9f8cd2f1282c4dc7ae9a4bea155e2661~mv2.jpg#originWidth=602&originHeight=773"
41 * },
42 * "displayed": true,
43 * "custom": false
44 * },
45 * "memberId": "4b9f4b64-0792-481e-9289-b2550c1bb7ea",
46 * "minutesToRead": 1,
47 * "moderationDetails": {},
48 * "pinned": false,
49 * "preview": false,
50 * "pricingPlanIds": [
51 * "b6e94a0c-4d0f-435e-9602-0dd61d2aca37"
52 * ],
53 * "relatedPostIds": [
54 * "425a5dca-c32d-40e6-b2d7-a8ffa3addded"
55 * ],
56 * "slug": "my-vacation",
57 * "tagIds": [
58 * "b698f939-cab5-419b-9966-ba0fa3316de9"
59 * ],
60 * "title": "My vacation",
61 * "translationId": "3cd710b7-c28d-4547-9b8a-3c1ec776064b"
62 * }
63 * }
64 */
65
Get a post by slug with additional fields

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { posts } from 'wix-blog-backend';
3
4/* Sample slug value:
5 * 'my-slug'
6 * Sample options value:
7 * {
8 * fieldsets: [
9 * 'URL',
10 * 'CONTENT_TEXT'
11 * ]
12 * }
13 */
14
15export const getPostBySlugFunction = webMethod(Permissions.Anyone, async (slug, options) => {
16 try {
17 const result = await posts.getPostBySlug(slug, options);
18 const title = result.post.title;
19 const postId = result.post._id;
20 console.log('Retrieved Result:', result);
21 return result;
22 } catch (error) {
23 console.error(error);
24 }
25});
26
27/* Promise resolves to:
28 * {
29 * "post": {
30 * "_id": "ccbb6257-ed0e-4521-97df-8b5b207adb00",
31 * "categoryIds": [
32 * "1ea22fce-bc3c-4b78-9422-f0f367f8628e"
33 * ],
34 * "commentingEnabled": true,
35 * "contentText": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading. #sea #sun",
36 * "excerpt": "Create a blog post subtitle that summarizes your post in a few short, punchy sentences and entices your audience to continue reading....",
37 * "featured": true,
38 * "firstPublishedDate": "2020-08-05T21:00:00.000Z",
39 * "hashtags": [
40 * "sea",
41 * "sun"
42 * ],
43 * "heroImage": ""
44 * "language": "en",
45 * "lastPublishedDate": "2020-08-05T21:00:00.000Z",
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": 1,
55 * "moderationDetails": {},
56 * "pinned": false,
57 * "preview": false,
58 * "pricingPlanIds": [
59 * "b6e94a0c-4d0f-435e-9602-0dd61d2aca37"
60 * ],
61 * "relatedPostIds": [
62 * "425a5dca-c32d-40e6-b2d7-a8ffa3addded"
63 * ],
64 * "slug": "my-vacation",
65 * "tagIds": [
66 * "b698f939-cab5-419b-9966-ba0fa3316de9"
67 * ],
68 * "title": "My vacation",
69 * "translationId": "3cd710b7-c28d-4547-9b8a-3c1ec776064b",
70 * "url": "http://https://tadasz7.wixsite.com/blog-velo-events/post/my-vacation"
71 * }
72 * }
73 */
74