Woocommerce orderby with meta_query
-
I’m trying to get a custom sorting (by date out of toolset wp-types) for the woocommerce shop page, it’s sorting by the custom date but I want to have the products with “release dates” that are in the last week and upcoming week only. So if the date is more than 1 week away I don’t want it to show.
The meta_query & query is working outside of this sorting function but can’t get it to work inside this sorting function. See my code:
add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args'); function custom_woocommerce_get_catalog_ordering_args( $args ) { global $wp_query; $lastweek = strtotime("-1 week"); $nextweek = strtotime("+1 week"); if (isset($_GET['orderby'])) { switch ($_GET['orderby']) : case 'lastUpcomingWeek' : $args['order'] = 'DESC'; $args['meta_key'] = 'wpcf-date-555a971b'; $args['orderby'] = 'meta_value_num'; $args['meta_query'] = array( array( 'key' => 'wpcf-date-555a971b', 'value' => array($lastweek, $nextweek), 'compare' => 'BETWEEN' ) ); break; endswitch; } return $args; } add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby'); function custom_woocommerce_catalog_orderby( $sortby ) { $sortby['lastUpcomingWeek'] = 'Last week & Upcoming week'; return $sortby; } add_action( 'save_post', 'save_woocommerce_attr_to_meta' ); function save_woocommerce_attr_to_meta( $post_id ) { foreach( $_REQUEST['attribute_names'] as $index => $value ) { update_post_meta( $post_id, $value, $_REQUEST['attribute_values'][$index] ); } }
Hope someone can help..
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Woocommerce orderby with meta_query’ is closed to new replies.