ClassifiedEngine: Sorting Price By Lowest to Highest

Updated on November 14, 2024 in No Category
0 on December 4, 2014

With current version of ClassifiedEngine, when you search a products, you can sort price all the results by highest to lowest.

But can we sort price all products by lowest to highest? The answer is YES.

Here is your result is going to look like at front end:

sort price

To do that, you need to add these following codes into child-theme/functions.php file

add_filter( 'ce_ad_custom_sort_args' , 'ce_ad_custom_sort_args_child' );

function ce_ad_custom_sort_args_child($args){

   $args['price_asc'] = array( 'label' =>   __("Price Asc", ET_DOMAIN),  'key'   => __("price_asc", ET_DOMAIN));

   return $args;

}

add_filter( 'pre_get_posts', 'pre_get_posts_child', 112 );

function pre_get_posts_child($query) {
   global $wpdb,$wp_query,$et_global;
   if ( !$query->is_main_query()  ) return $query;
   $orderby    =    ce_get_ad_orderby();

   if ($orderby == "price_asc"){

       $query->set('order','ASC');

       $query->set('meta_key','et_price');

       add_filter( 'posts_orderby', 'posts_orderby_asc');

    }

    return $query;

}

function posts_orderby_asc() {

global $wpdb;

// return "{$wpdb->postmeta}.meta_value DESC";

return "{$wpdb->postmeta}.meta_value + 0 ASC, {$wpdb->posts}.post_date DESC";

}
 
  • Liked by
Reply