Conversation
supportLove your plugin so far! -Quick question though, if I wanted to sort a page using WP_Query using the average (‘avrg’) star rating, how would I do so? https://wordpress.org/plugins/acf-starrating/
Honestly when I created the plugin, I had not thought about sorting (I forgot about it and I didn’t want to create a lot of fields) ((. In the next release, I planned to think about adding fields in the “meta” tables. Now there are two ways to sort the data (they both are not good): 1. Sort the data array after loading from the database. 2. The use of the filter posts_order (with MYSQL string functions). Example: <?php // WP_Query: $args = array( 'post_type' => 'ideas', 'meta_key' => 'ideas_rating', 'numberposts' => '-1' ); add_filter( 'posts_orderby', 'filter_rating_query'); $query = new WP_Query; $my_posts = $query->query($args); remove_filter( 'posts_orderby', 'filter_rating_query'); foreach( $my_posts as $my_post ){ print $my_post->post_title; the_field( 'ideas_rating', $my_post->ID ); } // Filter function: function filter_rating_query( $query ) { global $wpdb; $query = ' SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(' . $wpdb->postmeta . '.meta_value, \'\"\', \'\'), \';s:5:votes\', 1), \':\', -1) DESC'; return $query; } ?>
Thanks for the response!
Honestly when I created the plugin, I had not thought about sorting (I forgot about it and I didn’t want to create a lot of fields) ((. In the next release, I planned to think about adding fields in the “meta” tables. Now there are two ways to sort the data (they both are not good): 1. Sort the data array after loading from the database. 2. The use of the filter posts_order (with MYSQL string functions). Example: <?php // WP_Query: $args = array( 'post_type' => 'ideas', 'meta_key' => 'ideas_rating', 'numberposts' => '-1' ); add_filter( 'posts_orderby', 'filter_rating_query'); $query = new WP_Query; $my_posts = $query->query($args); remove_filter( 'posts_orderby', 'filter_rating_query'); foreach( $my_posts as $my_post ){ print $my_post->post_title; the_field( 'ideas_rating', $my_post->ID ); } // Filter function: function filter_rating_query( $query ) { global $wpdb; $query = ' SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(' . $wpdb->postmeta . '.meta_value, \'\"\', \'\'), \';s:5:votes\', 1), \':\', -1) DESC'; return $query; } ?>
Thanks for the response!