WordPress API – count posts

Is there a way to count the number of posts / pages via the WordPress API?

I’m wanting to insert a post then check, using the API, that the count has gone up by 1.

I’ve looked at http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts

2 Answers
2

Assuming you are using Linux or OS X, the easiest way is probably to use wp-cli (if present in your WordPress installation) to return a list of all posts:

wp-cli post list

Then pipe it to the word count tool to get the number of lines:

wc -l

Finally, deduct one to take care of the header line which is not a post:

awk '{print $1-1}'

So, in one line:

wp-cli post list | wc -l | awk '{print $1-1}'

Leave a Comment