Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Timber getting posts of a custom post type

I'm just digging into WordPress plus Timber and I came upon a problem that I can't resolve.

I have created a custom post type called "project", within which I created a custom field called "project_category". That custom field contains a checkbox of two choices (graphic, web design).

The question is what can I do to display all the projects that contains the project_category "graphic"?

Here is how I started:

graphic.php template

I created a graphic.php file with those wp queries:

$context = Timber::get_context();

$args = array(
    // Get post type project
    'post_type' => 'project',
    // Get all posts
    'posts_per_page' => -1,
    // Gest post by "graphic" category
    'meta_query' => array(
        array(
            'key' => 'project_category',
            'value' => 'graphic',
            'compare' => 'LIKE'
        )
    ),
    // Order by post date
    'orderby' => array(
        'date' => 'DESC'
    ),
);

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

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

graphic.twig Then I create a twig file with this loop.

{% extends "base.twig" %}

{% block content %}

<div class="l-container">

    <main role="main">
        <div class="l-row">
            <h1>My graphic design projects</h1>

            {% for post in posts %}

                <a href="{{ post.link }}" class="project-images l-col l-col--1-of-4 l-col--m-1-of-2">
                    <h2>{{ post.title }}</h2>

                        {% if post.thumbnail %}
                            <img src="{{post.get_thumbnail.src('medium_large')}}" alt="{{post.title}}" />
                        {% endif %}
                </a>

            {% endfor %}
        </div> 
    </main>

</div>

{% endblock %}

With this solution I can get only one project. When I want to display more than one project the project doesn't show up. I tried to use "for post in projects" or "for post in post.projects", but nothing worked out really.

What can I do to display all the projects that contains the project_category "graphic"?

like image 214
filnug Avatar asked Oct 25 '16 12:10

filnug


People also ask

How do I query a custom post type?

Querying by Post Type You can query posts of a specific type by passing the post_type key in the arguments array of the WP_Query class constructor. This loops through the latest ten product posts and displays the title and content of them one by one.

How do I get custom post type data in WordPress?

First, you can simply go to Appearance » Menus and add a custom link to your menu. This custom link is the link to your custom post type. Don't forget to replace 'example.com' with your own domain name and 'movies' with your custom post type name.

How can I see all custom post types?

As luck would have it, WordPress has a get_post_types() function that returns the names of all standard post types and custom post types. So, to find out all the post types available to us, we'll use the get_post_types() function along with the print_r() function in PHP to return and display a result.

How are custom post types different from regular posts?

A custom post type is nothing more than a regular post with a different post_type value in the database. The post type of regular posts is post , pages use page , attachments use attachment and so on. You can now create your own to indicate the type of content created.


1 Answers

@filnug, you're almost there. I think there's just some confusion about sending vars from PHP to Twig:

graphic.php:

$context = Timber::get_context();
$args = array(
// Get post type project
'post_type' => 'project',
// Get all posts
'posts_per_page' => -1,
// Gest post by "graphic" category
'meta_query' => array(
    array(
        'key' => 'project_category',
        'value' => 'graphic',
        'compare' => 'LIKE'
    )
),
// Order by post date
'orderby' => array(
    'date' => 'DESC'
));

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

twig file:

{% for post in graphics %}
    <h2>{{ post.title }}</h2>
    (other markup goes here)

{% endfor %}

best of luck!

like image 71
Jared Avatar answered Oct 19 '22 19:10

Jared