Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: Count Posts to Rest API
Description: Counts the post types and add them to the rest API
Plugin URI: https://webbiz.ie
Author: Jason Morton
Author URI: https://webbiz.ie
Version: 1.0
License: GPL2
Text Domain: count-posts
*/
/*
Copyright (C) 2016 Jason Morton info@webbiz.ie
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
*
* Count all the posts in the blog and return the number to the REST API
*
*/
function wb_count_posts() {
$post_types = array();
$count_posts = array();
// Get an array of Registered Post Types
foreach ( get_post_types( '', 'names' ) as $post_type ) {
// Push PT names to an array
array_push( $post_types, $post_type );
}
// Get the count of each post type
foreach ($post_types as $post_type ) {
// Push PT names to an array
array_push( $count_posts, wp_count_posts($post_type) );
}
// Combine the two Arrays KEY(Post Type), VALUE (Post Count)
$data = array_combine($post_types, $count_posts);
return $data;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'count-posts/v1', '/count', array(
'methods' => 'GET',
'callback' => 'wb_count_posts',
) );
} );
@WebbizAdmin

This comment has been minimized.

Copy link
Owner Author

@WebbizAdmin WebbizAdmin commented Jul 1, 2016

Googled how to get the count of a posts type from the WP Rest API v2 and found a lot of messy stuff, I think this is quite clean. Any feed back let me know. Add this to your Plugins folder and activate in the backend. Then point to http://domain.com/wp-json/count-posts/v1/count, This will show an object off all CPT and core in an object.
{ post: { publish: "76", future: 0, draft: 0, pending: 0, private: 0, trash: "1", auto-draft: "1", inherit: 0 }, page: { publish: "15", future: 0, draft: 0, pending: 0, private: 0, trash: 0, auto-draft: 0, inherit: 0 }, attachment: { publish: 0, future: 0, draft: 0, pending: 0, private: 0, trash: 0, auto-draft: 0, inherit: "26" }, revision: { publish: 0, future: 0, draft: 0, pending: 0, private: 0, trash: 0, auto-draft: 0, inherit: "2" }, nav_menu_item: { publish: "26", future: 0, draft: 0, pending: 0, private: 0, trash: 0, auto-draft: 0, inherit: 0 }, book: { publish: "1", future: 0, draft: 0, pending: 0, private: 0, trash: 0, auto-draft: 0, inherit: 0 } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment