Skip to content

Instantly share code, notes, and snippets.

@kadamwhite
Created May 28, 2014 18:09
Show Gist options
  • Save kadamwhite/c2604627aadb5649242a to your computer and use it in GitHub Desktop.
Save kadamwhite/c2604627aadb5649242a to your computer and use it in GitHub Desktop.
If I try to call $this->get_post_type inside a filter function, I get a 502 bad gateway error.
diff --git a/lib/class-wp-json-posts.php b/lib/class-wp-json-posts.php
index 941c796..599e8af 100644
--- a/lib/class-wp-json-posts.php
+++ b/lib/class-wp-json-posts.php
@@ -1090,6 +1090,23 @@ class WP_JSON_Posts {
}
/**
+ * Embed post type data into taxonomy data
+ *
+ * @uses self::get_post_type()
+ * @param array $data Taxonomy data
+ * @param array $taxonomy Internal taxonomy data
+ * @return array Filtered data
+ */
+ public function add_post_type_data( $data, $taxonomy ) {
+ foreach( $taxonomy->object_type as $type ) {
+ // $data['types'][ $type ] = $type; // This works
+ $data['types'][ $type ] = $this->get_post_type( $type ); // This doesn't: Why?
+ }
+
+ return $data;
+ }
+
+ /**
* Helper method for {@see new_post} and {@see edit_post}, containing shared logic.
*
* @since 3.4.0
diff --git a/lib/class-wp-json-taxonomies.php b/lib/class-wp-json-taxonomies.php
index 4ca4767..539ea29 100644
--- a/lib/class-wp-json-taxonomies.php
+++ b/lib/class-wp-json-taxonomies.php
@@ -105,7 +105,7 @@ class WP_JSON_Taxonomies {
$data['meta']['links']['collection'] = json_url( $base_url );
}
- return apply_filters( 'json_prepare_taxonomy', $data );
+ return apply_filters( 'json_prepare_taxonomy', $data, $taxonomy );
}
/**
diff --git a/plugin.php b/plugin.php
index 3571b04..dad44dc 100644
--- a/plugin.php
+++ b/plugin.php
@@ -75,6 +75,7 @@ function json_api_default_filters($server) {
// Posts
$wp_json_posts = new WP_JSON_Posts($server);
add_filter( 'json_endpoints', array( $wp_json_posts, 'register_routes' ), 0 );
+ add_filter( 'json_prepare_taxonomy', array( $wp_json_posts, 'add_post_type_data' ), 10, 2 );
// Users
$wp_json_users = new WP_JSON_Users($server);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment