简体   繁体   中英

Tricky Query: Nested Ordering by two values

I want to order results by two database values: rating_full followed by rating_count

Currently, Im ordering by the highest rating_full. it works fine.

$sql .= " LEFT JOIN {$wpdb->postmeta} rating ON ({$wpdb->posts}.ID = rating.post_id AND rating.meta_key IN ('**rating_full**'))";
…

…

$sql = "cast(rating.meta_value as decimal(10,2)) {$order}";
……

The first line of code, part of the SELECT statement, retrieves the rating_full part

The second line of code is the ORDER B Y part, which currently just uses the rating_count

As far as I can tell rating.meta_value referred to in the second line of code is the rating_full value

I'm trying to get it to ORDER BY rating_full , rating_count I'm not sure how to modify the first line so that I can achieve this.

Thanks

FULL CODE:

// Sorting
add_filter('posts_join', 'directorySortingJoin',10,2);
function directorySortingJoin($join, $query) {
global $wpdb, $aThemeOptions;
if ($query->is_main_query() && !$query->is_admin &&   ((isset($_GET['dir-search'])) || (isset($query->query_vars["a-dir-item-category"])) || (isset($query->query_vars["a-dir-item-location"])))) {
$sql = "";
 // default ordering
 $orderby = (isset($aThemeOptions->directory->defaultOrderby)) ? $aThemeOptions->directory->defaultOrderby : 'post_date';
 // get from get parameters
 if (!empty($_GET['orderby'])) {
    $orderby = $_GET['orderby'];
}
 if ($orderby == 'rating') {
    **$sql .= " LEFT JOIN {$wpdb->postmeta} rating ON   ({$wpdb->posts}.ID =  rating.post_id AND rating.meta_key IN ('rating_full'))";**

    //$sql .= " LEFT JOIN {$wpdb->postmeta} rating ON (wp_posts.ID = rating.post_id AND rating.meta_key IN ('rating_full')) LEFT JOIN {$wpdb->postmeta} count ON ({$wpdb->posts} = count.post_id AND count.meta_key IN  ('rating_count'))";


 }
 if ($orderby == 'packages') {
    directorySaveUserPackagesToDb();
    $sql .= " LEFT JOIN {$wpdb->usermeta} packages ON ({$wpdb->posts}.post_author = packages.user_id AND packages.meta_key IN ('dir_package'))";
}
 if (isset($aThemeOptions->directory->showFeaturedItemsFirst)) {
    $sql .= " LEFT JOIN {$wpdb->postmeta} featured ON ({$wpdb->posts}.ID =  featured.post_id AND featured.meta_key IN ('dir_featured'))";
 }
$join .= $sql;
//echo $join;
 }
return $join;

 }
   add_filter('posts_orderby', 'directorySortingOrderby',10,2);
   function directorySortingOrderby($orderby, $query) {
   global $wpdb, $aThemeOptions;
   if ($query->is_main_query() && !$query->is_admin && ((isset($_GET['dir-search'])) || (isset($query->query_vars["a-dir-item-category"])) || (isset($query->query_vars["a-dir-item-location"])))) {
  $sql = "";
  // default ordering
  $orderby = (isset($aThemeOptions->directory->defaultOrderby)) ? $aThemeOptions->directory->defaultOrderby : 'post_date';
$order = (isset($aThemeOptions->directory->defaultOrder)) ?   $aThemeOptions->directory->defaultOrder : 'DESC';
   // get from get parameters
    if (!empty($_GET['orderby'])) {
    $orderby = $_GET['orderby'];
   }
 if (!empty($_GET['order'])) {
    $order = $_GET['order'];
 }
if ($orderby == 'rating') {
     if (isset($aThemeOptions->directory->showFeaturedItemsFirst)) {
        $sql = "featured.meta_value DESC, convert(rating.meta_value, decimal) {$order}";
     } else {
         //$sql = "convert(rating.meta_value, decimal)    {$order}";

        **$sql = "cast(rating.meta_value as   decimal(10,2)) {$order}";**
        //$sql = "cast(rating.meta_value as decimal(10,2)) {$order}, count.meta_value {$order}";

    }
 } else if ($orderby == 'packages') {
    if (isset($aThemeOptions->directory->showFeaturedItemsFirst)) {
        $sql = "featured.meta_value DESC, packages.meta_value {$order}";
    } else {
        $sql = "packages.meta_value {$order}";
    }
} else {
    if (isset($aThemeOptions->directory->showFeaturedItemsFirst)) {
        $sql = "featured.meta_value DESC, {$wpdb->posts}.{$orderby}   {$order}";
    }
  }
   $orderby = $sql;
   //echo $orderby;
 }
 return $orderby;
 }
// Save directory packages for sorting
function directorySaveUserPackagesToDb() {
$users = get_users();
// capabilities list
$roles = array(
   'administrator' => 10,
   'directory_5' => 9,
'directory_4' => 8,
'directory_3' => 7,
'directory_2' => 6,
'directory_1' => 5,
'editor' => 4,
'author' => 3,
'contributor' => 2,
'subscriber' => 1
);
 foreach ($users as $user) {
  if (isset($user->roles[0])) {
    if (array_key_exists($user->roles[0], $roles)) {
        update_user_meta($user->ID, 'dir_package',    $roles[$user- >roles[0]]);
    } else {
        update_user_meta($user->ID, 'dir_package',   0);
    }
   }
 }
  }

Here is the solution i had typos

$sql .= " LEFT JOIN {$wpdb->postmeta} rating ON ({$wpdb->posts}.ID = rating.post_id AND rating.meta_key IN ('rating_full')) LEFT JOIN {$wpdb->postmeta} count ON ({$wpdb->posts}.ID = count.post_id AND count.meta_key IN ('rating_count'))";

$sql = "cast(rating.meta_value as decimal(10,2)) {$order}, cast(count.meta_value as decimal(10,2)) {$order}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM