REST API: Display Category names in JSON?

For my needs, I customized the wp rest posts callback: function get_all_posts( $data, $post, $context ) { return [ ‘id’ => $data->data[‘id’], ‘date’ => $data->data[‘date’], ‘date_gmt’ => $data->data[‘date_gmt’], ‘modified’ => $data->data[‘modified’], ‘title’ => $data->data[‘title’][‘rendered’], ‘content’ => $data->data[‘content’][‘rendered’], ‘excerpt’ => $data->data[‘excerpt’][‘rendered’], ‘category’ => get_the_category_by_ID( $data->data[‘categories’][0] ), ‘link’ => $data->data[‘link’], ]; } add_filter( ‘rest_prepare_post’, ‘get_all_posts’, 10, 3 … Read more

wordpress JSONAPI introspector always limits number at 10?

I’m answering my own question because it might be useful to someone else sometime. After reading through the somewhat sparse documentation on the wordpress plugin directory and getting nowhere. I found a variable in the JSON API code here’s how it works PS -1 just mean “all”: $json_api->query->count=-1; $posts = $json_api->introspector->get_posts(array(‘post_type’ => array(‘post’,’tweet’,’gallery’,’video’,’music’), ‘post_parent’ => … Read more

attachment media-template data model (data.size.url)

Problem fixed: I was not sending all properties (and didn’t find a list of possible properties) that the media_template expects. Finally I ended up alerting the data object by doing this (last 6 lines), where I first checked out ‘data’, and then each [object, object] that was returned (such as data.size) <script type=”text/html” id=”tmpl-attachment”> <div … Read more

Using WP_Query to grab custom meta values, foreach to json object

The best way to go about this is with $wpdb. The query to draw out meta_value by category is: $query = $wpdb->get_results( “SELECT p.`ID`, pm.`meta_value` FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} `p` ON `p`.`ID` = pm.`post_id` LEFT JOIN {$wpdb->term_relationships} `tr` ON `p`.`ID` = `tr`.`object_id` LEFT JOIN {$wpdb->term_taxonomy} `tt` ON `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id` WHERE `pm`.`meta_key` = … Read more

Custom url in wordpress

You just need to have a handler function that is called before content is sent to the browser. Without knowing exactly what you are trying to do, here is a general function that will work: function my_plugin_json_handler(){ /*First you should check the POST/GET Request for some variable that tells the plugin that this is a … Read more