My site actually has people's names as product titles and I really need it to sort alphabetically by the second name (surname)

I think I've found solutions to enable this with default WP post titles but not woocommerce.

Is it possible to amend this code to get it working with Woocommerce products?

I found the following HERE

function posts_orderby_lastname ($orderby_statement) 
$orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
return $orderby_statement;

Which the author said works (but I have no means to test it) and also the following HERE

function posts_orderby_lastname ($orderby_statement)
$orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
return $orderby_statement;

add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
'post_type' => 'artists',
'showposts' => -1,
'order' => 'ASC'
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
<div class="block item">
<a href="<?php the_permalink(); ?>">
<div class="inner">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
<div class="block-content">
<h4><?php the_title(); ?></h4>

remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );
wp_reset_query(); // Restore global post data stomped by the_post().

The php open and closing tags cause errors when pasting into the child theme functions but I removed the top opener & this saved without error but not sure that's correct??

I'm not a coder so don't really know where to start. Is it possible to use similar code above to achieve this with woocommerce 'product' custom post type?

Thanks very much in advance (once again : )

  • Why don't you change the title to Surname Name? Seems like a simpler solution than running terribly slow queries.
    – sandrodz
    Feb 13, 2018 at 20:29
  • Thanks for your reply. Yes, that would be simple to do but unfortunately doesn't achieve what I need, which is to order by the second word. Reversing the titles, making peoples names backwards wouldn't be correct for this.
    – Blix
    Feb 16, 2018 at 16:37

1 Answer 1


I've achieved this in a slightly different way.

In the single product edit page, Woocommerce (WC) has the ability to create custom fields. (if you can't see the 'custom fields' area in the edit product page, select 'screen options' at the top right of the wordpress admin page and check 'custom fields')

So, I created a 'surname' custom field on one product, exported all products via the built in WC export functionality, so I could see where the one new 'surname' field was on a csv spreadsheet.

Then, within the spreadsheet, I copied all of the second words into the new custom field and then imported it back in, which populated every product with the new custom field and their corresponding value.

Once that was done, I found a couple of snippets of code that I add to my functions.php. This added the 'surname' field to the 'orderby' sorting.

 * 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 'surname':
        $sort_args['orderby']  = 'meta_value';
        // Sort by meta_value because we're using alphabetic sorting
        $sort_args['order']    = 'asc';
        $sort_args['meta_key'] = 'surname';
        // use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"

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['surname'] = __( 'Sort by surname', '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' );

 * This code should be added to functions.php of your theme
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby'); function am_woocommerce_catalog_orderby( $args ) { $args['meta_key'] = 'surname'; $args['orderby'] = 'meta_value'; $args['order'] = 'asc'; return $args; }

If anyone else uses this, you can name your custom field whatever you want, within the edit product page of WC. Just make sure that you change all instances of 'surname' in the code above to whatever you do use.

It then occurred to me that I could do the same thing with the sku code:make this the second word then sort by it.

However, I can't get this to work. I'm using the WOOF product filter plugin which also has to be modified to make the custom field sorting work. I think this is the problem.

I'm sure I'll work it out though and the above will work with regular WC stores. Hope that helps someone else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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