WordPress: Get Search Results

Template
Function
1
Template
Function
by
Voiceflow Community

Request to `/search` endpoint to retrieve results from a post title search query.

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Voiceflow Community
This is some text inside of a div block.
Overview
This is some text inside of a div block.
by
This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Heading
Based in
This is some text inside of a div block.
Heading

Function Code Snippet


export default async function main(args) {
// Input variables from Voiceflow
const { wpDomain, wpApikey, wpUsername, wpSearchString } = args.inputVars;

// Custom function to encode a string to Base64
function base64Encode(str) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
let encoded = '', i = 0;

while (i < str.length) {
const a = str.charCodeAt(i++);
const b = i < str.length ? str.charCodeAt(i++) : 0;
const c = i < str.length ? str.charCodeAt(i++) : 0;

const b1 = (a >> 2) & 0x3F;
const b2 = ((a & 0x03) << 4) | ((b >> 4) & 0x0F);
const b3 = ((b & 0x0F) << 2) | ((c >> 6) & 0x03);
const b4 = c & 0x3F;

if (!i) {
encoded += chars.charAt(b1) + chars.charAt(b2) + '==';
} else if (i == str.length + 1) {
encoded += chars.charAt(b1) + chars.charAt(b2) + chars.charAt(b3) + '=';
} else {
encoded += chars.charAt(b1) + chars.charAt(b2) + chars.charAt(b3) + chars.charAt(b4);
}
}

return encoded;
}

// Encode credentials using the custom function
const credentials = `${wpUsername}:${wpApikey}`;
const base64Credentials = base64Encode(credentials);

// Define the API URL with parameters
const baseUrl = `https://${wpDomain}/wp-json/wp/v2/search`;
const queryParams = `?type=post&subtype=post&search=${encodeURIComponent(wpSearchString)}`;
const apiUrl = baseUrl + queryParams;

// Configuration for fetch request
const config = {
method: 'GET',
headers: {
"Authorization": `Basic ${base64Credentials}`,
"Content-Type": "application/json"
}
};

try {
const response = await fetch(apiUrl, config);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const posts = await response.json;

if (!posts || posts.length === 0) {
throw new Error("No posts found");
}

// Format the fetched posts into a bullet list with titles and IDs
const titles = posts.map(post => `• ${post.title} (${post.id})`).join('\n');

return {
outputVars: {
wpPostTitles: titles
},
next: { path: 'success' },
trace: [{ type: "text", payload: { message: `Posts successfully fetched. Here are the titles:\n${titles}` }}]
};

} catch (error) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Error:" + error.message } }]
};
}
}
copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.