Support

Account

Home Forums Backend Issues (wp-admin) Sort by a customfield while showing all posts Reply To: Sort by a customfield while showing all posts

  • Here’s the code from functions.php

    add_action( 'pre_get_posts', 'vendor_sort' );
    function vendor_sort( WP_Query $wp_query ) {
        // Ensure it is the global $wp_query
        if ( ! $wp_query->is_main_query() )
            return;
        
        if ( $wp_query->is_tax( 'vendor-type' ) ) {
            $wp_query->set( 'meta_key', 'crfp-average-rating' );
            $wp_query->set( 'orderby', 'meta_value_num' );
            $wp_query->set( 'meta_compare', '>' );
            $wp_query->set( 'order', 'DESC' );
        }
    }