WordPress.org

Ready to get started?Download WordPress

Forums

Relevanssi - A Better Search
[resolved] reverse sort relevanssi_hits_filter meta_value (2 posts)

  1. maxhodges
    Member
    Posted 1 year ago #

    I have this relevanssi_hits_filter for ordering by a meta "likes" value. Can someone help me reverse the orderby?

    http://pastebin.com/cVVAkAH5

    add_filter('relevanssi_hits_filter', 'order_the_results');
    
    function order_the_results($hits) {
        global $wp_query;
        if ($wp_query->query_vars['orderby'] == 'likes') {
            $likes = array();
            foreach ($hits[0] as $hit) {
            	$likecount = get_post_meta($hit->ID, '_likes', true);
            	if (!isset($likes[$likecount])) $likes[$likecount] = array();
            	array_push($likes[$likecount], $hit);
            }
            ksort($likes);
            $sorted_hits = array();
            foreach ($likes as $likecount => $year_hits) {
            	$sorted_hits = array_merge($sorted_hits, $year_hits);
            }
            $hits[0] = $sorted_hits;
        }
        return $hits;
    }

    http://wordpress.org/extend/plugins/relevanssi/

  2. maxhodges
    Member
    Posted 1 year ago #

    got it krsort()

    Maybe useful: here's my relevanssi_hits_filter

    can sort (asc and desc) by
    -a meta_value (called '_likes' in my case)
    -wordpress total_comments count

    http://pastebin.com/ZiMzMw4F

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags