If you using woo-commerce and want to sort product by custom field then follow the below example code, it helpful for doing your work easy.
<?php add_filter('woocommerce_get_catalog_ordering_args', 'wh_catalog_ordering_args'); function wh_catalog_ordering_args($args) { global $wp_query; $args['order'] = 'DESC'; $args['meta_key'] = 'posted_date'; $args['orderby'] = 'meta_value'; return $args; } ?>
Question and answer is powered by AnsPress.io