Quick: PHP Database Query - Sorting wordpress posts by rating

Selesai Dipasang Apr 6, 2015 Dibayar saat pengiriman
Selesai Dibayar saat pengiriman

Hi,

I would like someone to solve this issue I'm having.

[url removed, login to view]

It should be a quick job, syntax issue.

Update: I completed a lot of the project myself. But I need one small issue fixed.


What needs to be done:

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 BY 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.

<br>
Thanks


**FULL CODE:**

<?php

// 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);
}
}
}
}

Pemrosesan Data MySQL PHP

ID Proyek: #7438822

Tentang proyek

8 proposal Proyek online Aktif Apr 8, 2015

Diberikan kepada:

VtSaha

Hello, I can fix this quickly in less than an hour. Bid is 8 EUR. I look forward to work for you. Regards, Rizwan

€12 EUR dalam 1 hari
(1 Ulasan)
0.0

8 freelancer rata-rata menawar €16 untuk pekerjaan ini

rajeshsonisl

Hello! With 99% completion rate, 750 successfully completed projects, and a 5.00 reputation (maximum possible, 5.0) (Yes, not even 4.99 average rating, can be verified on my profile page https://www.freelancer.com/u Lebih banyak

€22 EUR dalam 1 hari
(570 Ulasan)
7.7
rexzetsolutions

i didnt cheeck the code, i just see the image and read the issue, i am waitng for your reply to start the work

€30 EUR dalam 1 hari
(108 Ulasan)
5.9
DreamOne

A proposal has not yet been provided

€14 EUR dalam 1 hari
(25 Ulasan)
4.5
BitLabs

Dear sir, I will complete your project asap, LIFETIME error free guarantee and provide ONE YEAR support... I have gone through your outsourced requirement "Quick: PHP Database Query - Sorting wordpress posts by rat Lebih banyak

€10 EUR dalam 1 hari
(29 Ulasan)
4.2
AzmatM

Hi, my bid is finalized until your project description and real work remains same. I got your point and i can do this project well before my quoted time. I hope you will consider my proposal and have a look over my Lebih banyak

€18 EUR dalam 0 hari
(15 Ulasan)
4.2
adrianwsna

As You said it will be easy task to do. I'm working as a php programmer and do MySQL queries everyday. I need to get reputation on freelancer so this is good opportunity. You won't be disappointed.

€14 EUR dalam 1 hari
(0 Ulasan)
0.0