Home › Forums › Add-ons › Repeater Field › Order by subfield values
Hi everyone, I got a problem I don’t manage to solve.
I want to display on a page a list of posts. Each of this post contains a repeater field with a start date and an end date (it’s a list of shows for a festival).
I got a repeater field called “show” with 3 subfields for each row :
I manage to get the list of all posts and order them by startdate using this following code:
<?php
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'show_%", "meta_key LIKE 'show_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
$the_query = new WP_Query(array(
'post_type' => 'artist',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'show_%_startdate',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>
<ul class="schedule">
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
if( have_rows('show',$post->ID) ): ?>
<?php
while( have_rows('show',$post->ID) ): the_row(); ?>
<?php if( get_sub_field('show_startdate') != '' ): ?>
<li class="schedule-item"><?php the_title(); ?> -
<?php the_sub_field('show_day'); ?> /
<?php the_sub_field('show_startdate'); ?> /
<?php the_sub_field('show_enddate'); ?>
</li>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile; ?>
</ul>
<?php wp_reset_query(); ?>
The only problem I got is that one artist (1 artist = 1 post) can produce himself more than once on the same date. And I don’t manage to sort my posts list on every value of the startdate subfield. It’s sorted only on the first value.
For the moment I got:
Artist 1 – 4pm
Artist 2 – 6pm
Artist 2 – 10pm
Artist 3 – 8pm
Where I would like to get:
Artist 1 – 4pm
Artist 2 – 6pm
Artist 3 – 8pm
Artist 2 – 10pm
Is there a way to do that?
WordPress 4.2 introduced new order by clauses for meta_query.
https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
Which would probably let you do what you looking for.
By the way, very good solution for altering ‘meta_key =’ to ‘meta_key LIKE’. I know I”l be using that in the future.
The topic ‘Order by subfield values’ is closed to new replies.
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.