WordPress.org

Forums

WP Ultimate Search
Order posts by postmeta, meta_key, meta_value (4 posts)

  1. markedwardford
    Member
    Posted 2 years ago #

    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/

  2. markedwardford
    Member
    Posted 2 years ago #

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

  3. markedwardford
    Member
    Posted 2 years ago #

    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); ?>
  4. markedwardford
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP Ultimate Search
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.