How Can I Create a New WordPress Post Using Node.js?

This topic was automatically generated from Slack. You can find the original thread here.

How I can create a new WordPress post using Node.js step?

To create a new WordPress post using a Node.js step in Pipedream, you’ll need to use the WordPress REST API. The following example demonstrates how you can achieve this. Before you start, ensure you have the necessary credentials (e.g., WordPress site URL, username, and application password) to authenticate your requests. import { axios } from "@pipedream/platform" export default defineComponent({ props: { wordpressSiteUrl: { type: "string", label: "WordPress Site URL", description: "The URL of your WordPress site (e.g., https://yourwordpresssite.com)", }, username: { type: "string", label: "Username", description: "The username to authenticate the request.", }, applicationPassword: { type: "string", label: "Application Password", description: "The application password to authenticate the request.", }, postTitle: { type: "string", label: "Post Title", description: "The title of the WordPress post.", }, postContent: { type: "string", label: "Post Content", description: "The content of the WordPress post.", }, postStatus: { type: "string", label: "Post Status", description: "The status of the post (e.g., 'publish', 'draft').", optional: true, }, }, async run({ $ }) { const authString = Buffer.from(`${this.username}:${this.applicationPassword}`).toString('base64'); const response = await axios($, { method: "POST", url: `${this.wordpressSiteUrl}/wp-json/wp/v2/posts`, headers: { "Authorization": `Basic ${authString}`, "Content-Type": "application/json", }, data: { title: this.postTitle, content: this.postContent, status: this.postStatus || 'publish', }, }); return response; }, }) This code defines a Pipedream component with properties to configure the WordPress site URL, authentication credentials, post title, post content, and post status. It then makes a POST request to the WordPress REST API to create a new post with the specified details. Please replace the placeholder values in the props with your actual WordPress site details and credentials. The postStatus

How to add arrguments like categories and slug?

To provide a detailed answer, I would need more context about where you’re trying to add arguments like categories and slug. Are you trying to add these as parameters to an API request, or are you looking to include them in a Pipedream component or workflow? If you’re working with a Pipedream component or workflow and need to pass these as parameters to an API, you could include them in the axios request within a Node.js code step. Here’s a basic example of how you might structure this if you’re making a GET request and want to include categories and slug as query parameters: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ async run({ steps, $ }) { const categories = “yourCategory”; // Replace with your actual category const slug = “yourSlug”; // Replace with your actual slug return await axios($, { url: https://yourapi.com/data?categories=${categories}&slug=${slug}, method: “GET”, // Include any necessary headers here headers: { “Authorization”: “Bearer YOUR_API_KEY”, }, }); }, });