Support

Account

Home Forums General Issues I need order by my ACF field in woocommerce_get_catalog_ordering_args

Unread

I need order by my ACF field in woocommerce_get_catalog_ordering_args

  • Hi, I need order by my ACF field in woocommerce and I dont know how. My field name is “capacidad”

    My code is:

    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 ‘capacidad_asc’:
    $sort_arg[‘orderby’] = get_field( “capacidad” );
    // Sort by meta_value because we’re using alphabetic sorting
    $sort_args[‘order’] = ‘asc’;
    $sort_args[‘meta_key’] = ‘capacidad’;
    // use the meta key you’ve set for your custom field, i.e., something like “location” or “_wholesale_price”
    break;

    }

    return $sort_args;
    }
    add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘skyverge_add_postmeta_ordering_args’ );

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.