Custom post type group by meta_value
-
I’ve tried to solve this problem by myself since two days, but nothing worked and a thousand examples didn’t gave me the answer.
My situation is easy: I have a defined custom post type named scripts with different fields, one of them “video_number”. There are several entries with a value e.g. 70 for this “video_number”. Understandably there are many other video numbers with each a few entries. What I want to achieve is a list, grouped by the video_number.
The list should look like:
video 70
entry 4
entry 5
entry 6video 75
entry 10
entry 11
entry 12…
Now I don’t know how to realize this with PHP. I’ve studied the documentation of ACF, but all I achieved was a normal list of all entries.
Actualy the code looks like:
$the_query = new WP_Query(array( 'post_type' => 'scripts', 'posts_per_page' => -1, 'meta_key' => 'video_number', 'orderby' => 'meta_value_num', 'order' => 'ASC' )); ?> <?php if( $the_query->have_posts() ): ?> <ul> <?php while( $the_query->have_posts() ) : $the_query->the_post(); $class = get_field('video_number'); ?> <li class="<?php echo $class; ?>"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; ?> </ul> <?php endif; ?> <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>I know I have to implement another loop, but it seems I still have to learn many things in PHP.
I appreciate any help. Thanks!
Adriano
The topic ‘Custom post type group by meta_value’ is closed to new replies.