• Resolved artiastudio

    (@artiastudio)


    Hi,

    I keep searching for a way to achieve this but can’t find a solution.

    I’m using the Relevanssi plugin (which works great by the way) and would like to sort the search results by one custom field.

    Example:

    All posts that have the “on” value for the “featured” custom field should be displayed on top of the results..

    Any ideas?

    I’ve had a look and tried to implement the weights filter but no success..

    Thanks

    https://wordpress.org/plugins/relevanssi/

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

    (@msaari)

    That would be easiest to do with relevanssi_hits_filter. It would go like this:

    add_filter('relevanssi_hits_filter', 'featured_on_first');
    function featured_on_first($hits) {
        $featured = array();
        $not_featured = array();
        foreach ( $hits[0] as $hit ) {
            $is_featured = get_post_meta( $hit->ID, 'featured', true );
            'on' == $is_featured ? $featured[] = $hit : $not_featured[] = $hit;
        }
        $hits[0] = array_merge( $featured, $not_featured );
        return $hits;
    }

    Add this to your theme functions.php, for example.

    Thread Starter artiastudio

    (@artiastudio)

    Thank you!

    It worked.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Order results by custom fields’ is closed to new replies.