Skip to content

Instantly share code, notes, and snippets.

@nateluzod
Created April 21, 2021 01:51
Show Gist options
  • Save nateluzod/dad6d3650cb138f6560b5619c939876d to your computer and use it in GitHub Desktop.
Save nateluzod/dad6d3650cb138f6560b5619c939876d to your computer and use it in GitHub Desktop.
Timber/Twig Filter - get content by term
<?php
/**
* Get content by taxonomy
* Sorts in reverse-cron
* @param category {integer} the ID of the term we're pulling from
* @param postType {string, array} the post type(s) we're drawing from
* @param numberPosts {integer} the number of posts we'd like back
*/
add_filter('get_twig', function( $twig ) {
$get_content_by_term = new Twig_SimpleFunction( 'get_content_by_term', function (
$category = null,
$postType = 'blog',
$numberPosts = 10
) {
$args = [
'post_type' => $postType,
'numberposts' => $numberPosts,
'category' => $category,
'orderby' => 'date',
'order' => 'DESC'
];
$posts = get_posts($args);
return $posts;
});
$twig->addFunction( $get_content_by_term );
return $twig;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment