Last active
June 24, 2016 13:41
-
-
Save yoren/95cd7779de9cc6e4c189 to your computer and use it in GitHub Desktop.
Add posts to term JSON data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function my_json_prepare_term( $data, $term, $context ) { | |
global $wp_query; | |
$route = $wp_query->query['json_route']; | |
if ( ! preg_match( '/(terms\/.+)/', $route) ) | |
return $data; | |
$args = array( | |
'tax_query' => array( | |
array( | |
'taxonomy' => $term->taxonomy, | |
'field' => 'slug', | |
'terms' => $term->slug | |
) | |
), | |
'posts_per_page' => 5 | |
); | |
$posts = get_posts( $args ); | |
$posts_arr = array(); | |
foreach ( $posts as $p ) { | |
$posts_arr[] = array( | |
'ID' => $p->ID, | |
'title' => $p->post_title | |
); | |
} | |
$data['posts'] = $posts_arr; | |
return $data; | |
} | |
add_filter( 'json_prepare_term', 'my_json_prepare_term', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment