The WordPress API has no function available to do this. You can do one of things:
- Use
$wpdb
and create a custom SQL table to retrieve terms for posts published within the last 12 hours. The only problem with this approach is that if (unlikely anytime soon) WordPress were to changes its databse scheme you may have to update the SQL statement. - Use
get_posts()
to get posts published within the last 12 hours, loop through them and pull out their terms. This disadvantage here is that its slightly less efficient than (1).
(2) is fairly straightforward to do, so I’ll leave out the details. The following is a (completely untested) attempt of (1):
function wpse144088_get_latest_terms(){
$last_12_hours = new DateTime( '-12 hours' );
$sql = $wpdb->prepare("
SELECT DISTINCT t.*
FROM {$wpdb->terms} AS t
INNER JOIN {$wpdb->term_taxonomy} AS tt
ON t.term_id = tt.term_id
JOIN {$wpdb->term_relationships} AS tr
ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN {$wpdb->posts} AS p
ON p.ID = tr.object_id
WHERE tt.taxonomy IN ('cause')
AND p.post_type="reasons"
AND p.post_status="publish"
AND p.post_date_gmt > %s
ORDER BY t.name ASC;",
$last_12_hours->format( 'Y-m-d H:i:s' )
);
$recent_terms = $wpdb->get_results( $sql );
return $recent_terms;
}