Created
February 6, 2023 14:29
-
-
Save titomus/dca4b0038693b1c543cff6415ed6dd21 to your computer and use it in GitHub Desktop.
Utiliser Moment et WPapi pour publier en masse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const moment = require("moment"); | |
var WPAPI = require("wpapi"); | |
const blogUrl = "https://site.fr/"; | |
const login = "LOGIN"; | |
const password = "PASS"; | |
const cat = 3; // id de votre catégorie | |
//console.log(`date 1: ${dateToPost} ensuite: ${nextPostDate}`); | |
// authentification | |
var wp = new WPAPI({ | |
endpoint: `${blogUrl}/wp-json`, | |
// This assumes you are using basic auth, as described further below | |
username: login, | |
password: password, | |
}); | |
var fileContents = fs.readFileSync("./articles.txt", "utf8"); | |
//console.log(fileContents); | |
const articles = fileContents.split("+++"); | |
// count articles | |
const postNum = articles.length; | |
const startDateToPost = moment().subtract(120, 'days').format(); | |
for (var i = 0; i < postNum-1; i++) { | |
let article = articles[i].split("###"); | |
let title = article[0]; | |
if(title === '') {continue;} | |
let content = article[1]; | |
title = title.charAt(0).toUpperCase() + title.slice(1); | |
wp.posts() | |
.create({ | |
// "title" and "content" are the only required properties | |
title: title, | |
content: content, | |
categories: [cat], | |
// Post will be created as a draft by default if a specific "status" | |
// is not specified | |
status: "publish", | |
date: moment(startDateToPost).add(7*i, 'days').format(), | |
}) | |
.then(function (response) { | |
// "response" will hold all properties of your newly-created post, | |
// including the unique `id` the post was assigned on creation | |
console.log(`Successfully created post #${response.id}`); | |
}) | |
.catch(function (err) { | |
// Something went wrong! | |
console.log(err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment