Home › Forums › General Issues › Sort posts by meta key query
Hi,
I’m looking to sort multiple posts into lists after the query.
I have to query the posts using a taxonomy term that is a field and then have all these posts sorted in different year by an other field.
I want to have lists with the year as title like this:
2021:
post
post
post
2020:
post
post
post
etc.
Here’s my code right now:
<?php $topics = get_sub_field('sous-categories-en-avant');
if ($topics) {
if (!is_array($topics)) {
$topics = array($topics->slug);
}
$args = array(
'post_type' => 'production',
'posts_per_page' => -1,
'order' => 'DESC',
'meta_key' => '_production_year',
'orderby' => 'meta_value',
'tax_query' => array(
array(
'taxonomy' => 'production-category',
'field' => 'slug',
'terms' => $topics,
),
),
);
$production_query = new WP_Query($args);
if ($production_query->have_posts()) $count = 0; {?>
<div class="grid" data-packery='{ "itemSelector": ".grid-item", "gutter": 10 }'>
<?php while($production_query->have_posts()) {
$production_query->the_post(); ?>
<?php $year = get_field('_production_year');?>
<?php if ($count > 0 && (($count % 2) == 0)) {?>
<div class="grid-item">
<?php } else{?>
<div class="grid-item grid-item--width2">
<?php }?>
<a href="<?php the_permalink();?>">
<div class="productions-archives-thumb">
<?php the_post_thumbnail( 'medium','');?>
</div>
<div class="productions-archives-text">
<?php the_title();?>
<?php the_field('_production_year');?>
</div>
</a>
</div>
<?php $count++; }?>
</div><!-- grid-->
<?php }
wp_reset_postdata();
}?>
I hope that someone can help me with this, thanks by advance, best
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.