I have a plugin which calculates shipping per product and stores in table

named wp_gph84r36td_pbs. I have added shipping column using custom hook and got shipping rate there. But now i am trying to apply sorting there. I have return custom hook for that as well but its not working.It returns no result.

Here is my sorting code:

function sort_shipping($clauses, $wp_query){
        global $wpdb;

        if(isset($wp_query->query['orderby']) && $wp_query->query['orderby'] == 'shipping'){

            $clauses['join'] .= <<<SQL
RIGHT JOIN wp_gph84r36td_pbs ON {$wpdb->posts}.ID=wp_gph84r36td_pbs.product_id

SQL;


  print_r($clauses);          //$clauses['orderby'] = "ORDER BY {$wpdb->pbs}.item_cost ASC)";


            if(strtoupper($wp_query->get('order')) == 'ASC'){
                $clauses['orderby'] .= 'ASC';
            } else{
                $clauses['orderby'] .= 'DESC';
            }
        }
        return $clauses;
    }
    add_filter('posts_clauses', 'sort_shipping', 10, 2);

When in print_r it retruns this, but with no result.

 wp_gph84r36td_posts.post_type = 'product' AND (wp_gph84r36td_posts.post_status = 'publish' OR wp_gph84r36td_posts.post_status = 'closed' OR wp_gph84r36td_posts.post_status = 'future' OR wp_gph84r36td_posts.post_status = 'draft' OR wp_gph84r36td_posts.post_status = 'pending' OR wp_gph84r36td_posts.post_status = 'private' OR wp_gph84r36td_posts.post_status = 'hidden') [groupby] => [join] => RIGHT JOIN wp_gph84r36td_pbs ON wp_gph84r36td_posts.ID=wp_gph84r36td_pbs.product_id [orderby] => wp_gph84r36td_posts.post_date ASC [distinct] => [fields] => wp_gph84r36td_posts.* [limits] => LIMIT 0, 20 ) 

How ever my custom mysql works

SELECT *
FROM wp_gph84r36td_posts
RIGHT JOIN wp_gph84r36td_pbs ON 

wp_gph84r36td_posts.ID = 

wp_gph84r36td_pbs.product_id ORDER BY 

wp_gph84r36td_pbs.item_cost ASC

Read more here: custom column with sorting in post type product


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: