Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add multiple post type support #163

Merged
merged 3 commits into from

2 participants

This page is out of date. Refresh to see the latest.
Showing with 13 additions and 7 deletions.
  1. +13 −7 lib/class-wp-json-posts.php
View
20 lib/class-wp-json-posts.php
@@ -73,18 +73,24 @@ public function register_routes( $routes ) {
* @see WP_JSON_Posts::get_post() for more on $fields
* @see get_posts() for more on $filter values
*
- * @param array $filter optional
- * @param array $fields optional
- * @return array contains a collection of Post entities.
+ * @param array $filter Parameters to pass through to `WP_Query`
+ * @param string $context
+ * @param string|array $type Post type slug, or array of slugs
+ * @param int $page Page number (1-indexed)
+ * @return stdClass[] Collection of Post entities
*/
public function get_posts( $filter = array(), $context = 'view', $type = 'post', $page = 1 ) {
$query = array();
- $post_type = get_post_type_object( $type );
- if ( ! ( (bool) $post_type ) || ! $post_type->show_in_json )
- return new WP_Error( 'json_invalid_post_type', __( 'The post type specified is not valid' ), array( 'status' => 403 ) );
+ // Validate post types and permissions
+ $query['post_type'] = array();
+ foreach ( (array) $type as $type_name ) {
+ $post_type = get_post_type_object( $type_name );
+ if ( ! ( (bool) $post_type ) || ! $post_type->show_in_json )
+ return new WP_Error( 'json_invalid_post_type', sprintf( __( 'The post type "%s" is not valid' ), $type_name ), array( 'status' => 403 ) );
- $query['post_type'] = $post_type->name;
+ $query['post_type'][] = $post_type->name;
+ }
global $wp;
// Allow the same as normal WP
Something went wrong with that request. Please try again.