Skip to content

Instantly share code, notes, and snippets.

Created February 6, 2023 14:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save titomus/dca4b0038693b1c543cff6415ed6dd21 to your computer and use it in GitHub Desktop.
Utiliser Moment et WPapi pour publier en masse
const fs = require("fs");
const moment = require("moment");
var WPAPI = require("wpapi");
const blogUrl = "";
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");
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);
// "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 #${}`);
.catch(function (err) {
// Something went wrong!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment