Group by a custom value field wordpress posts
-
I have several posts with a custom field named “series”. I want to group all posts by this custom field and below of this i want to list all posts which have not this custom field.
First i wanted to get grouped custom field value and then to be able to query again for all posts with this custom value key and value. But even trying to get the unique custom values does not work.
What i tried is this:
<?php function query_group_by_filter($groupby){ global $wpdb; return $wpdb->postmeta . '.meta_key = "series"'; } ?> <?php add_filter('posts_groupby', 'query_group_by_filter'); ?> <?php $states = new WP_Query(array( 'meta_key' => 'series', 'ignore_sticky_posts' => 1 )); ?> <?php remove_filter('posts_groupby', 'query_group_by_filter'); ?> <ul> <?php while ( $states->have_posts() ) : $states->the_post(); $mykey_values = get_post_custom_values( 'series' ); foreach ( $mykey_values as $key => $value ) { echo "<li>$key => $value ( 'series' )</li>"; } endwhile; ?> </ul>
Whats wrong with this code?
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Group by a custom value field wordpress posts’ is closed to new replies.