Created
April 21, 2021 01:51
-
-
Save nateluzod/dad6d3650cb138f6560b5619c939876d to your computer and use it in GitHub Desktop.
Timber/Twig Filter - get content by term
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 | |
/** | |
* 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