Skip to content

Instantly share code, notes, and snippets.

@yoren

yoren/functions.php

Last active Jun 24, 2016
Embed
What would you like to do?
Add posts to term JSON data
<?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