Code js automatically repost old posts on wordpress every 15 minutes

code js automatically repost old posts on wordpress

To automatically repost old posts on WordPress every 15 minutes using JavaScript, you can use the WordPress REST API to retrieve the list of published posts and then use JavaScript’s setInterval function to schedule a function to run every 15 minutes to repost the oldest post in the list. Here is an example code snippet:

// Retrieve the list of published posts using the WordPress REST API
fetch(‘https://example.com/wp-json/wp/v2/posts?status=publish’)
.then(response => response.json())
.then(posts => {
// Schedule a function to repost the oldest post every 15 minutes
setInterval(() => {
// Sort the posts by their published date, in ascending order
posts.sort((a, b) => new Date(a.date) – new Date(b.date));
// Get the oldest post
const oldestPost = posts[0];
// Duplicate the post with a new publish date
fetch(‘https://example.com/wp-json/wp/v2/posts’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: ‘Bearer YOUR_ACCESS_TOKEN’ // Replace with your own access token
},
body: JSON.stringify({
title: oldestPost.title.rendered,
content: oldestPost.content.rendered,
date: new Date().toISOString(), // Set the publish date to now
status: ‘publish’
})
})
.then(response => response.json())
.then(newPost => {
console.log(`Reposted post “${oldestPost.title.rendered}” as post ID ${newPost.id}`);
})
.catch(error => {
console.error(`Error reposting post “${oldestPost.title.rendered}”:`, error);
});
}, 15 * 60 * 1000); // Schedule the function to run every 15 minutes
})
.catch(error => {
console.error(‘Error retrieving list of published posts:’, error);
});

Note that you will need to replace example.com with your own WordPress site’s domain and YOUR_ACCESS_TOKEN with a valid access token for the WordPress REST API. You can obtain an access token by following the instructions in the WordPress REST API documentation.

For Category another

To repost the old posts under a specific category in WordPress, you can modify the previous code snippet to set the category of the new post using the categories parameter in the request body.

Here is an updated code snippet that reposts the oldest post under a specific category every 15 minutes:

// Set the category ID of the desired category
const categoryId = 1;

// Retrieve the list of published posts using the WordPress REST API
fetch(‘https://example.com/wp-json/wp/v2/posts?status=publish’)
.then(response => response.json())
.then(posts => {
// Schedule a function to repost the oldest post under the desired category every 15 minutes
setInterval(() => {
// Filter the posts by the desired category and sort them by their published date, in ascending order
const categoryPosts = posts.filter(post => post.categories.includes(categoryId));
categoryPosts.sort((a, b) => new Date(a.date) – new Date(b.date));
// Get the oldest post
const oldestPost = categoryPosts[0];
// Duplicate the post with a new publish date and set the category to the desired category
fetch(‘https://example.com/wp-json/wp/v2/posts’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: ‘Bearer YOUR_ACCESS_TOKEN’ // Replace with your own access token
},
body: JSON.stringify({
title: oldestPost.title.rendered,
content: oldestPost.content.rendered,
date: new Date().toISOString(), // Set the publish date to now
status: ‘publish’,
categories: [categoryId] })
})
.then(response => response.json())
.then(newPost => {
console.log(`Reposted post “${oldestPost.title.rendered}” as post ID ${newPost.id} under category ${categoryId}`);
})
.catch(error => {
console.error(`Error reposting post “${oldestPost.title.rendered}” under category ${categoryId}:`, error);
});
}, 15 * 60 * 1000); // Schedule the function to run every 15 minutes
})
.catch(error => {
console.error(‘Error retrieving list of published posts:’, error);
});

In this code snippet, replace 1 with the ID of the desired category. The categories parameter in the request body is an array of category IDs, so you can add more categories by including additional IDs in the array.

Leave a Reply

Your email address will not be published. Required fields are marked *