Support » Plugin: WP Ultimate Search » Order posts by postmeta, meta_key, meta_value

  • Hello there
    I have found the wpus-results-template.php and have tried to change the order of the posts in order of name to start off with but no joy.
    What I would like to do is order them by 2 meta_key values
    crfp-average-rating and crfptotal-ratings
    Can anybody help me with this please?
    I feel like banging my head against a brick wall.

    https://wordpress.org/plugins/wp-ultimate-search/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Is the way forward creating a function???
    I have been discussing this with many people and not one of them have been able to successfully come up with a solution to order the posts by the meta value. I can see the meta values as options to search by but not sure how we can sort the order of the posts in the template if the query has already been carried out and set beforehand.
    Please can anyone shed any light???

    I’ve tried this

    <?php if ( $wpus_results->have_posts() ) : ?>
    
    	<?php while ( $wpus_results->have_posts() ) : $wpus_results->the_post(); ?>
    
    <?php $wpus_results = get_query_var('wpus_results'); ?>
    <?php $paged = intval(get_query_var('paged')); ?>
    <?php $paged = ($paged) ? $paged : 1; ?>
    <?php $args = array(
       'wpus_results' => $wpus_results,
       'paged' => $paged,
       'more' => $more = 0,
    'posts_per_page'	=> -1,
    'orderby'			=> meta_value_num,
    'meta_key'			=> crfp-total-ratings,
       'order' => 'DESC',
    ); ?>
    <?php query_posts($args); ?>
    function serviceproviderratings($query) {
        if ( !is_admin() && $query->wpusquery() ) {
            if ( $query->query_vars['post_type'] == 'service-provider' ) {
        	$query->set('orderby', 'meta_value_num');
                $query->set( 'meta_key', 'crfp-total-ratings' );
            }
        }
    }
    add_action( 'pre_get_posts', 'serviceproviderratings' );

    Also tried this with no results

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Order posts by postmeta, meta_key, meta_value’ is closed to new replies.