• Resolved Yury

    (@telitsin)


    Timber does not show custom type posts in posts index page.

    index.php:

    $context = Timber::get_context();
    $context['posts'] = Timber::get_posts();
    $context['foo'] = 'bar';
    $templates = array('index.twig');
    if (is_home()){
      array_unshift($templates, 'home.twig');
    }
    Timber::render($templates, $context);

    functions.php:

    function register_post_types(){
      register_post_type( 'test',
        array(
          'labels' => array(
            'name' => __( 'test' ),
            'singular_name' => __( 'test' )
          ),
          'public' => true,
          'taxonomies' => array( 'post_tag', 'category' )
        )
      );
    }

    https://www.ads-software.com/plugins/timber-library/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author jarednova

    (@jarednova)

    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' );
    Plugin Author jarednova

    (@jarednova)

    yup, that’ll do it to!

    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom type posts are missing’ is closed to new replies.