With the current version of ClassifiedEngine, when you search a product, you can sort by price from by highest to lowest.

But can we sort products from the lowest price to the highest? The answer is YES.

Here is what your result looks like in the front end:

sort price

To do this, 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";

}
7 Comments
  • Dane
    Dec  07TH,  2014

    Does not work for me unfortunately keep getting this error:

    Parse error: syntax error, unexpected ‘Asc”’ (T_STRING) in /wp-content/themes/classifiedengine-child/functions.php on line 14

    • An
      Dec  08TH,  2014

      Hi Dane,

      Would you mind heading to our forum? Our developer here will help you fix this issue.

      enginethemes.com/forums/

      Regards,

  • Momo
    Dec  10TH,  2014

    Hi, are you gonna to implement this in your theme as an option we can easy tick ?

    • An
      Dec  10TH,  2014

      Hi Momo,

      Actually, we haven’t had any plan to directly implement in our theme yet. However, you can just follow this tutorial then it’ll work.

      Regards,

      • Momo
        Mar  25TH,  2016

        Hello An,
        As we are now in 2016, I would like to understand why this option has not been implemented to the theme ?
        I guess you know this should not be an option but an integrated feature for a classified theme.
        I don’t really understand why instead of implementing this small piece of code to the main theme you ask your customers to add this code by their own in their theme.
        As you already have the code, for you it’s not a pain to implement it definitively in the theme.
        You are asking all your customers to follow the tutorial when for you it requires to do it only 1 time.
        Please understand it’s not a disrespect asking you that but as now I am the owner of your theme, I would really appreciate to understand why you ask us to follow your instructions instead of simply implementing good things like that knowing you care of your customers.
        Regards,
        Momo

  • Zelu
    Apr  01ST,  2016

    No code to sort prices works in the current version of classifiedengine – Could include this feature in the next update? Thank you.

    • Uyen Tran
      Apr  12TH,  2016

      Hello Zelu,
      Sorry for the late response.
      Could you please create a new ticket at our forum forum.enginethemes.com? Our technical support will check the possibility and provide the code that you can use to customize your site as your requirement.
      Thanks a lot.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.