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


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}'
  • I was hoping there would be some basic method in WP-CLI (e.g. like wp-cli post count) that did the job but this is very helpful - thanks. – Snowcrash Apr 13 '16 at 8:28

With WP-CLI installed from https://wp-cli.org/ you can retrieve the total post count by using:

wp post list --format=count

Full documentation for the POST LIST command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.