Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author lienann

    (@lienann)

    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;
    
    }
    ?>

    Thread Starter oniontaylor

    (@oniontaylor)

    Thanks for the response!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WP_Query’ is closed to new replies.