I think you might not need multiple loops to get your data into your template.
As I understood it, it’s a matter of sorting. You want to sort your posts so that posts with premium-franchisor
set to 1 appear first. You can already do this using WP_Query. This is how your arguments array could look like:
$args = array(
// Get all posts
\'posts_per_page\' => -1,
// Order by meta value first, then order by post date
\'orderby\' => array(
\'meta_value_num\' => \'DESC\',
\'date\' => \'DESC\'
),
// Select meta key which will be used for meta_value_num in orderby
\'meta_key\' => \'premium-franchisor\',
);
Concluding from the title of your question I guess that you want to use Timber together with Twig to bring your data into your template.
This is how you could do that:
<?php
$context = Timber::get_context();
$args = array(
// Get all posts
\'posts_per_page\' => -1,
// Order by meta value first, then order by post date
\'orderby\' => array(
\'meta_value_num\' => \'DESC\',
\'date\' => \'DESC\'
),
// Select meta key which will be used for meta_value_num in orderby
\'meta_key\' => \'premium-franchisor\',
);
$posts = Timber::get_posts( $args );
$context[\'posts\'] = $posts;
Timber::render( \'archive-agency.twig\', $context );
In Timber you use a context that you fill with the data you want to hand over to your template. To set up all the basic data Timber needs, we use get_context()
at the beginning. We then use Timber::get_posts()
to basically do a normal WP_Query with our customized args. Getting your post through Timber will make it easier to access post properties in your template.
You could display your posts like this:
<div class="post-preview">
<a href="{{ post.permalink }}">{{ post.title }}</a>
<span class="post-date">{{ post.date }}</span>
{% if attribute( post, \'premium-franchisor\') %}This is a premium post.{% endif %}
</div>
See, post.permalink
or post.title
are not properties you get when you run a normal WP_Query, but only if you query through Timber. Here you’ll find a list with methods and properties that you can use.
Using multiple loops in Timber
Since you asked how to do multiple loops, I will also show you an example of how you could achieve the same result you tried:
$context = Timber::get_context();
// Get all premium_posts
$premium_args = array(
\'meta_key\' => \'premium-franchisor\',
\'meta_value\' => \'1\',
\'posts_per_page\' => -1,
);
$premium_posts = Timber::get_posts( $premium_args );
// Get all other posts
$my_other_args = array(
\'meta_key\' => \'premium-franchisor\',
\'meta_value\' => \'0\',
\'posts_per_page\' => -1,
);
$my_other_posts = Timber::get_posts( $my_other_args );
// Merge the two post arrays into one and assign it to context
$posts = array_merge( $premium_posts, $my_other_posts );
$context[\'posts\'] = $posts;
Timber::render( \'index.twig\', $context );
When you get your posts through Timber, you don’t get an object that you have to loop over to get to your data, but you get a simple array of posts to work with. You can edit, extend, sort or in your example: merge them very easily.
A small remark on premium-franchisor
In your meta key premium-franchisor
you used a hyphen. I’d advise you to use an underscore instead of a hyphen because often, it’s easier to access a property. E.g. you wouldn’t have to use
{% if attribute(post, \'premium-franchisor\') %}{% endif %}
But could just use
{% if post.premium_franchisor %}{% endif %}
Even if this is a very late answer, I hope this will help you or others get going with Timber and Twig.