$orderby = $va_options->default_listing_home_sort;
$args['orderby'] = $orderby;
$args['va_orderby'] = $orderby;
switch ( $orderby ) {
case 'highest_rating':
$args['meta_key'] = 'rating_avg';
$args['orderby'] = 'meta_value, comment_count';
$args['order'] = 'desc';
break;
case 'most_ratings':
$args['orderby'] = 'comment_count';
$args['order'] = 'desc';
break;
case 'newest':
$args['order'] = 'desc';
break;
case 'recently_reviewed':
$result_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->comments c ON p.ID = c.`comment_post_ID` WHERE p.`post_type` = '%s' ORDER BY c.`comment_ID` DESC", VA_LISTING_PTYPE ) );
$args['orderby'] = 'post__in';
$args['post__in'] = $result_ids;
break;
case 'rand':
$args['orderby'] = 'rand';
break;
case 'title':
$args['orderby'] = 'title';
break;
case 'default':
default:
$args['meta_key'] = 'featured-home';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
$args['va-featured'] = true;
break;
}
$query = new WP_Query( $args );
return $query;
}
add_filter( 'posts_clauses', 'va_featured_random_sort', 10, 2 );
function va_featured_random_sort( $clauses, $wp_query ) {
global $wpdb, $va_options;
if ( $wp_query->get( 'va-featured' ) ) {
$post_types = array();
$post_types[] = VA_LISTING_PTYPE;
if ( $va_options->events_enabled ) {
$post_types[] = VA_EVENT_PTYPE;
}
$post_type = isset( $wp_query->query_vars['post_type'] ) && in_array( $wp_query->query_vars['post_type'], $post_types ) ? $wp_query->query_vars['post_type'] : VA_LISTING_PTYPE;
$featured_option = $post_type . 's_featured_sort';
switch ( $va_options->$featured_option ) {
case 'oldest':
$clauses['orderby'] .= ", $wpdb->posts.post_date ASC";
break;
case 'newest':
$clauses['orderby'] .= ", $wpdb->posts.post_date DESC";
break;
case 'random':
default:
$clauses['fields'] .= ", if( ( `$wpdb->postmeta`.meta_value > 0 ), ( `$wpdb->postmeta`.meta_value + RAND() ), 0 ) as _va_featured_hash ";
$clauses['orderby'] .= ", _va_featured_hash ASC, $wpdb->posts.post_date DESC";
break;
}
}
return $clauses;
}
There are currently 1 users browsing this thread. (0 members and 1 guests)