I am trying to add a custom sorting option for woo commerce . So I am using following code to add new sort option plus set it as default

meta key is '_am_cat_sort'

function sv_personagem_woocommerce_shop_ordering( $sort_args ) {
  $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

    if ( 'personagem' == $orderby_value ) {
        $sort_args['orderby'] = '_am_cat_sort';
        $sort_args['order'] = 'ASC';
        $sort_args['meta_key'] = '';

    return $sort_args;
add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_personagem_woocommerce_shop_ordering' );

function sv_custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['personagem'] = 'Sort by cat: personagem';
    return $sortby;
add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'sv_custom_woocommerce_catalog_orderby' );

add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');

function custom_default_catalog_orderby() {
     return 'personagem'; // Can also use title and price

Now following is the code I am using to add new meta field

add_action('add_meta_boxes', 'add_am_product_metaboxes');

function add_am_product_metaboxes() {
add_meta_box('wpt_am_product', __('Sort value'), 'wpt_am_product', 'product', 'normal', 'high', null);

function wpt_am_product(){
  // Add the HTML for the post meta
  global $post;
  // Noncename needed to verify where the data originated
  wp_nonce_field( 'oj-am_product-fields', 'oj-am_product-fields_wpnonce', false, true );
  // Get the location data if its already been entered 

  $am_cat_sort  = get_post_meta($post->ID,  '_am_cat_sort', true);
  $am_cat_sort  = (int)$am_cat_sort; 
  $terms = wp_get_post_terms( $post->ID, 'product_cat' );
  foreach ($terms as $term) {
  if(in_array(76, $myterms)){
    $am_cat_sort  = -99;
    $am_cat_sort  = 0;

  echo '<label >';?><?php _e( 'sort order' );?></label> 
  <?php echo '<br><textarea  name="_am_cat_sort" rows="4" cols="80">'.$am_cat_sort.'</textarea>'.'<br>'; 


But sorting is not working at all

Thanks in advance


There are tutorials online. For example - https://www.skyverge.com/blog/sort-woocommerce-products-custom-fields/

Here is the main code to put into functions - https://gist.github.com/bekarice/0df2b2d54d6ac8076f84

 * Adds WooCommerce catalog sorting options using postmeta, such as custom fields
 * Tutorial: http://www.skyverge.com/blog/sort-woocommerce-products-custom-fields/
function skyverge_add_postmeta_ordering_args( $sort_args ) {

$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
switch( $orderby_value ) {

    // Name your sortby key whatever you'd like; must correspond to the $sortby in the next function
    case 'location':
        $sort_args['orderby']  = 'meta_value';
        // Sort by meta_value because we're using alphabetic sorting
        $sort_args['order']    = 'asc';
        $sort_args['meta_key'] = 'location';
        // use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"

    case 'points_awarded':
        $sort_args['orderby'] = 'meta_value_num';
        // We use meta_value_num here because points are a number and we want to sort in numerical order
        $sort_args['order'] = 'desc';
        $sort_args['meta_key'] = 'points';


return $sort_args;
add_filter( 'woocommerce_get_catalog_ordering_args', 'skyverge_add_postmeta_ordering_args' );
// Add these new sorting arguments to the sortby options on the frontend
function skyverge_add_new_postmeta_orderby( $sortby ) {

    // Adjust the text as desired
    $sortby['location'] = __( 'Sort by location', 'woocommerce' );
    $sortby['points_awarded'] = __( 'Sort by points for purchase', 'woocommerce' );

    return $sortby;
add_filter( 'woocommerce_default_catalog_orderby_options', 'skyverge_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'skyverge_add_new_postmeta_orderby' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.