Changing the orderby
-
Hi there,
i am trying to change the order by function in wordpress to make it order by attributes low to high or high to low (that does not matter). So far i found this on the internet and i am trying to work from there:
/************* Add sorting by attributes **************/ /** * Defines the criteria for sorting with options defined in the method below */ add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args'); function custom_woocommerce_get_catalog_ordering_args( $args ) { global $wp_query; // Changed the $_SESSION to $_GET if (isset($_GET['orderby'])) { switch ($_GET['orderby']) : case 'pa_nominaal-toerental-omwmin' : $args['order'] = 'ASC'; $args['meta_key'] = 'pa_nominaal-toerental-omwmin'; $args['orderby'] = 'meta_value_num'; break; endswitch; } return $args; } /** * Adds the sorting options to dropdown list .. The logic/criteria is in the method above */ add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby'); function custom_woocommerce_catalog_orderby( $sortby ) { $sortby['pa_nominaal-toerental-omwmin'] = 'Sorteer op Toerental: laag naar hoog'; return $sortby; } /** * Save custom attributes as post's meta data as well so that we can use in sorting and searching */ add_action( 'save_post', 'save_woocommerce_attr_to_meta' ); function save_woocommerce_attr_to_meta( $post_id ) { // Get the attribute_names .. For each element get the index and the name of the attribute // Then use the index to get the corresponding submitted value from the attribute_values array. foreach( $_REQUEST['attribute_names'] as $index => $value ) { update_post_meta( $post_id, $value, $_REQUEST['attribute_values'][$index] ); } } /************ End of Sorting ***************************/
The ‘pa_nominaal-toerental-omwmin’ is my attribute i am trying it out with but now if i work with this on the website it randomly filters on something but not sure what.
Any help here? ??
Viewing 14 replies - 1 through 14 (of 14 total)
Viewing 14 replies - 1 through 14 (of 14 total)
- The topic ‘Changing the orderby’ is closed to new replies.