Hi Yury, by default WP only returns posts in those queries (the get_posts function inherits those WP defaults). You would need to do something like:
$context['posts'] = Timber::get_posts('post_type=any');
//or
$context['posts'] = Timber::get_posts('post_type=test');
Thread Starter
Yury
(@telitsin)
Hi, jarednova.
Thanks for the reply.
I found a way to add posts of different types to query:
function test__pre_get_posts( $query ) {
if( is_home() && is_main_query() ) {
$query->set( 'post_type', array( 'post', 'test-post-type' ) );
}
}
add_action( 'pre_get_posts', 'test__pre_get_posts' );
Thread Starter
Yury
(@telitsin)
One more way:
1. Set different post types:
$context['posts'] = Timber::get_posts();
$context['slides'] = Timber::get_posts('post_type=slide');
$context['videos'] = Timber::get_posts('post_type=gallery_video');
2. Merge arrays in twig template:
{% set posts = posts|merge(slides) %}
{% set posts = posts|merge(videos) %}
Thread Starter
Yury
(@telitsin)
I have discovered that
$context['posts'] = Timber::get_posts('post_type=any');
//or
$context['posts'] = Timber::get_posts('post_type=test');
does not work for taxonomy archives. This way WP outputs all posts I have without filtering them for the taxonomy.
So in my case only this code works:
function test__pre_get_posts( $query ) {
if( !( is_post_type_archive() || is_admin() ) ) {
$query->set( 'post_type',
array(
'post', 'page', 'nav_menu_item', // Keep default types
'test-post-type',
'test-post-type-2'
)
);
}
}
add_action( 'pre_get_posts', 'test__pre_get_posts' );
NB! To preserve pages and menus to be shown post, page, nav_menu_item types have to be included.