Richard
Richard

Reputation: 190

Display Custom Post Type Category

I have set up a Custom Post Type called "tours" with a Taxomony "tour_category" Specific only to that Post Type. Inside the Taxomony I have set up 4 areas Golf Breaks, Walking Tours etc... What I am trying to achieve is render all the Posts in the Golf Breaks Taxomony into the golf.twig template however all I seem to get is a page not found error. Can someone point me in the right direction with this example.

$context = Timber::get_context();
$args = array(
// Get post type tours
    'post_type' => 'tours',
// Get all posts
    'posts_per_page' => -1,
// get post in "Golf Breaks" category
    'meta_query' => array(
        array(
            'key' => 'tour_category',
            'value' => 'Golf Breaks',
            'compare' => 'LIKE'
        )
    ),
// Order by post date
    'orderby' => array(
        'date' => 'DESC'
    ));

$context['Golf Breaks'] = Timber::get_posts( $args );

Timber::render( 'golf.twig', $context );

Upvotes: 0

Views: 610

Answers (1)

Jared
Jared

Reputation: 1734

@richard: you're going to have a very bad time with this line....

$context['Golf Breaks'] = Timber::get_posts( $args );

Since the property of the array is used by Twig, you want something with no spaces (and probably lowercase). Try....

$context['golf_breaks'] = Timber::get_posts( $args );

from there you can access the data in Twig with...

{% for post in golf_breaks %}
    <h1>{{ post.title }}</h1>
    <div>{{ post.content }}</div>
{% endfor %}

If you're not seeing anything, the problem is likely with fetching the posts from WP. Things with taxonomy queires can be tough, try debugging with just a get_posts to ensure WordPress is finding posts that match yr query

Upvotes: 1

Related Questions