Get Wordpress posts from site A, post latest to Wordpress site B

Hello all!

I’m experiencing the wonderful world of n8n! And already quite impressed!

However I’m doing something wrong to make my workflow fully work the way I want, suggestions and help are more than welcome:

It looks simple:

  1. check every x minutes if there is a new post of category Y on wordpress site A (via API)
  2. if there is a new post that never has been ‘processed’ before continue
  3. post a new post on wordpress site B (via API)

Step 1 and 3 are working already perfectly! The workflow looks like this:

Of course the tricky thing is the function node, I tried to reuse a Twitter version from another support thread but it doesn’t work :unamused::

const staticData = getWorkflowStaticData('global');
const newWPIds = items.map(item => item.json["ID"]);
const oldWPIds = staticData.oldWPIds; 

if (!oldWPIds) {
  staticData.oldWPIds = newWPIds;
  return items;
}


const actualNewWPIds = newWPIds.filter((id) => !oldWPIds.includes(id));
const actualNewPosts = items.filter((data) => actualNewWPIds.includes(data.json['ID']));
staticData.oldWPIds = [...actualNewWPIds, ...oldWPIds];

return actualNewPosts;

What do I have to do to make this work?

Thanks already for helping me out.

Kind regards,

Marcel.

Hey @imc67,

While I would normally dig out an answer for you that would use the function node what about just using an If node and checking the created / modified time of the post and if is newer than the last run time process it?

That is the process I tend to take with tweets and RSS feeds, So I would start by adding a Date & Time Node and if my Cron is every 5 minutes I would subtract 5 minutes from the current time then fetch the posts from Site A (or another service) and pop in an If and do an If the time from the Date & Time node is before the created / modified time we know it is new.

Hi @jon,

Thanks for the hints!

Found your Twitter version in the Workflows and recreated mine. The tests looks fine though I have to wait for a specific post on site A, it will be there in 14 hours so I wait and see!

2 Likes

Hey @imc67,

That is handy :slight_smile:

1 Like

@jon your suggestion works perfect!!!

I only noticed that the ‘reposting’ did worked but without the ‘featured image’ so that’s a next step to find out.

1 Like

Hi @imc67 can you share the whole workflow?

Thanks you!