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 slug with additional fields

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