How to Pull a Post from One Wordpress Site and Post it to Another?

  1. The postID is 361188 and it is an existing post.
  2. There does not appear to be an issue with the username or password because no errors are coming back.
  3. There does not appear to be an issue with the siteURL because no errors are coming back.
    Here is the actual data.
    https://www.sohh.com/wp-json/wp/v2/posts/361188

I see, thank you for providing the actual URL. I’ve checked the URL you provided and it seems to be publicly accessible and returns data without needing authentication. This could be the reason why you’re not seeing any errors related to the username or password. However, if the post data is still coming back empty, it might be due to the way the response is being handled in your code. In your current code, you’re returning response.data, but the axios function from @pipedream/platform directly returns the data from the HTTP response, not in a data property. Try modifying your code to return the response directly, like this: ```
import { axios } from “@pipedream/platform”; export default { props: { sourceSiteUrl: { type: “string”, label: “Source Site URL”, description: “The URL of the Wordpress site you want to pull posts from”, }, sourceSiteUsername: { type: “string”, label: “Source Site Username”, description: “The username for the source Wordpress site”, }, sourceSitePassword: { type: “string”, label: “Source Site Password”, description: “The password for the source Wordpress site”, }, postId: { type: “integer”, label: “Post ID”, description: “The ID of the post you want to pull from the source site”, }, }, async run({ $ }) { const response = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/posts/${this.postId}, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); return response; }, };

That code returned the following error:

Code Error
Command failed: pnpm install --prefer-offline --ignore-scripts -P

I’m sorry for the confusion. The error you’re seeing is not related to the Pipedream component code itself, but rather it seems to be an issue with the package installation process in your local development environment. In Pipedream, you don’t need to run pnpm install or any other package installation commands. All Node.js packages available on npm can be used directly in Pipedream code steps by importing them, and Pipedream will automatically handle the installation. If you’re trying to run the code locally, please ensure you have pnpm installed correctly. You can install it using npm install -g pnpm. If pnpm is installed and you’re still facing issues, it might be a problem with the specific package you’re trying to install. However, if you’re trying to run this code in a Pipedream workflow, you don’t need to worry about package installation. Just import the necessary packages at the top of your code and Pipedream will handle the rest. If you’re still having issues, please provide more details about your development environment and the exact steps you’re taking that lead to this error.

I went back to this code, and I am able to pull the row.

import { axios } from "@pipedream/platform";

export default {
  props: {
    sourceSiteUrl: {
      type: "string",
      label: "Source Site URL",
      description: "The URL of the Wordpress site you want to pull posts from",
    },
    sourceSiteUsername: {
      type: "string",
      label: "Source Site Username",
      description: "The username for the source Wordpress site",
    },
    sourceSitePassword: {
      type: "string",
      label: "Source Site Password",
      description: "The password for the source Wordpress site",
    },
    postId: {
      type: "integer",
      label: "Post ID",
      description: "The ID of the post you want to pull from the source site",
    },
  },
  async run({ $ }) {
    const response = await axios($, {
      method: "GET",
      url: `${this.sourceSiteUrl}/wp-json/wp/v2/posts/${this.postId}`,
      auth: {
        username: this.sourceSiteUsername,
        password: this.sourceSitePassword,
      },
    });
    return response;
  },
};

However, I noticed that the comments are being stripped from MySQL. Is there a way to keep those comments?

I’m glad to hear that you’re able to pull the post data successfully. Regarding the comments being stripped from MySQL, it’s important to note that the WordPress REST API and MySQL are separate systems. The WordPress REST API, which you’re using to fetch the post data, may not include comments in the response by default. To retrieve comments associated with a post, you typically need to make a separate request to the /comments endpoint, filtering by the post parameter. Here’s an example of how you might modify your code to also fetch comments for a given post: ```
import { axios } from “@pipedream/platform”; export default { props: { sourceSiteUrl: { type: “string”, label: “Source Site URL”, description: “The URL of the Wordpress site you want to pull posts from”, }, sourceSiteUsername: { type: “string”, label: “Source Site Username”, description: “The username for the source Wordpress site”, }, sourceSitePassword: { type: “string”, label: “Source Site Password”, description: “The password for the source Wordpress site”, }, postId: { type: “integer”, label: “Post ID”, description: “The ID of the post you want to pull from the source site”, }, }, async run({ $ }) { const postResponse = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/posts/${this.postId}, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); const commentsResponse = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/comments, params: { post: this.postId, }, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); return { post: postResponse, comments: commentsResponse, }; }, };

Can you modify this code to return an object containing both the post data and the associated comments?

import { axios } from "@pipedream/platform";

export default {
  props: {
    sourceSiteUrl: {
      type: "string",
      label: "Source Site URL",
      description: "The URL of the Wordpress site you want to pull posts from",
    },
    sourceSiteUsername: {
      type: "string",
      label: "Source Site Username",
      description: "The username for the source Wordpress site",
    },
    sourceSitePassword: {
      type: "string",
      label: "Source Site Password",
      description: "The password for the source Wordpress site",
    },
    postId: {
      type: "integer",
      label: "Post ID",
      description: "The ID of the post you want to pull from the source site",
    },
  },
  async run({ $ }) {
    const response = await axios($, {
      method: "GET",
      url: `${this.sourceSiteUrl}/wp-json/wp/v2/posts/${this.postId}`,
      auth: {
        username: this.sourceSiteUsername,
        password: this.sourceSitePassword,
      },
    });
    return response;
  },
};

Sure, I can help you modify the code to also fetch comments associated with the post. Here’s how you can do it: ```
import { axios } from “@pipedream/platform”; export default { props: { sourceSiteUrl: { type: “string”, label: “Source Site URL”, description: “The URL of the Wordpress site you want to pull posts from”, }, sourceSiteUsername: { type: “string”, label: “Source Site Username”, description: “The username for the source Wordpress site”, }, sourceSitePassword: { type: “string”, label: “Source Site Password”, description: “The password for the source Wordpress site”, }, postId: { type: “integer”, label: “Post ID”, description: “The ID of the post you want to pull from the source site”, }, }, async run({ $ }) { const postResponse = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/posts/${this.postId}, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); const commentsResponse = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/comments, params: { post: this.postId, }, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); return { post: postResponse, comments: commentsResponse, }; }, };

How can I pull the raw post_content field for the post?

To pull the raw post_content field for the post, you would need to modify the API endpoint to include the context query parameter with a value of edit. This will return more detailed information about the post, including the raw post_content. Here’s how you can modify your code: import { axios } from "@pipedream/platform"; export default { props: { sourceSiteUrl: { type: "string", label: "Source Site URL", description: "The URL of the Wordpress site you want to pull posts from", }, sourceSiteUsername: { type: "string", label: "Source Site Username", description: "The username for the source Wordpress site", }, sourceSitePassword: { type: "string", label: "Source Site Password", description: "The password for the source Wordpress site", }, postId: { type: "integer", label: "Post ID", description: "The ID of the post you want to pull from the source site", }, }, async run({ $ }) { const postResponse = await axios($, { method: "GET", url: `${this.sourceSiteUrl}/wp-json/wp/v2/posts/${this.postId}?context=edit`, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); const commentsResponse = await axios($, { method: "GET", url: `${this.sourceSiteUrl}/wp-json/wp/v2/comments`, params: { post: this.postId, }, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); return { post: postResponse, comments: commentsResponse, }; }, }; This code will fetch the post data with the raw post_content field and the associated comments. The results are then returned as an object containing both the post data and the comments.

Thank you so much, that worked.

Now what code can we use to push the object into the other Wordpress site?

Sure, to push the post and comments data to another WordPress site, you can use the WordPress REST API’s POST endpoints. Here’s an example of how you can modify your code to do this: ``` import { axios } from “@pipedream/platform”; export default { props: { sourceSiteUrl: { type: “string”, label: “Source Site URL”, description: “The URL of the Wordpress site you want to pull posts from”, }, sourceSiteUsername: { type: “string”, label: “Source Site Username”, description: “The username for the source Wordpress site”, }, sourceSitePassword: { type: “string”, label: “Source Site Password”, description: “The password for the source Wordpress site”, }, targetSiteUrl: { type: “string”, label: “Target Site URL”, description: “The URL of the Wordpress site you want to push posts to”, }, targetSiteUsername: { type: “string”, label: “Target Site Username”, description: “The username for the target Wordpress site”, }, targetSitePassword: { type: “string”, label: “Target Site Password”, description: “The password for the target Wordpress site”, }, postId: { type: “integer”, label: “Post ID”, description: “The ID of the post you want to pull from the source site”, }, }, async run({ $ }) { const postResponse = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/posts/${this.postId}?context=edit, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); const commentsResponse = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/comments, params: { post: this.postId, }, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); const postData = postResponse.data; const commentsData = commentsResponse.data; const newPostResponse = await axios($, { method: “POST”, url: `${this.targetSiteUrl}/wp

The code was cutoff.

I apologize for the cutoff. Here’s the complete code to push the post and comments data to another WordPress site: ```
import { axios } from “@pipedream/platform”; export default { props: { // … existing props targetSiteUrl: { type: “string”, label: “Target Site URL”, description: “The URL of the Wordpress site you want to push posts to”, }, targetSiteUsername: { type: “string”, label: “Target Site Username”, description: “The username for the target Wordpress site”, }, targetSitePassword: { type: “string”, label: “Target Site Password”, description: “The password for the target Wordpress site”, }, }, async run({ $ }) { // … existing code to fetch post and comments const newPostResponse = await axios($, { method: “POST”, url: ${this.targetSiteUrl}/wp-json/wp/v2/posts, auth: { username: this.targetSiteUsername, password: this.targetSitePassword, }, data: postData, }); const newPostId = newPostResponse.data.id; for (const comment of commentsData) { await axios($, { method: “POST”, url: ${this.targetSiteUrl}/wp-json/wp/v2/comments, auth: { username: this.targetSiteUsername, password: this.targetSitePassword, }, data: { …comment, post: newPostId, // associate comment with new post }, }); } return { newPost: newPostResponse.data }; }, };

The name of the component getting the data is code_pull. Please rewrite code to use that name.

Sure, I can modify the code to use code_pull as the component name. Here’s the updated code: ``` import { axios } from “@pipedream/platform”; export default { name: “code_pull”, props: { sourceSiteUrl: { type: “string”, label: “Source Site URL”, description: “The URL of the Wordpress site you want to pull posts from”, }, sourceSiteUsername: { type: “string”, label: “Source Site Username”, description: “The username for the source Wordpress site”, }, sourceSitePassword: { type: “string”, label: “Source Site Password”, description: “The password for the source Wordpress site”, }, targetSiteUrl: { type: “string”, label: “Target Site URL”, description: “The URL of the Wordpress site you want to push posts to”, }, targetSiteUsername: { type: “string”, label: “Target Site Username”, description: “The username for the target Wordpress site”, }, targetSitePassword: { type: “string”, label: “Target Site Password”, description: “The password for the target Wordpress site”, }, postId: { type: “integer”, label: “Post ID”, description: “The ID of the post you want to pull from the source site”, }, }, async run({ $ }) { const postResponse = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/posts/${this.postId}?context=edit, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); const commentsResponse = await axios($, { method: “GET”, url: ${this.sourceSiteUrl}/wp-json/wp/v2/comments, params: { post: this.postId, }, auth: { username: this.sourceSiteUsername, password: this.sourceSitePassword, }, }); const postData = postResponse.data; const commentsData = commentsResponse.data; const newPostResponse = await axios($, { method: “POST”, url: ${this.targetSiteUrl}/wp-json/wp/v2/posts, auth: {

The code is cut off.

I apologize for the inconvenience. Could you please provide more details about the code that was cut off? Specifically, I need to know the context or functionality of the code, and any specific part of the code that you have. This will help me provide a more accurate and complete response.