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;
}
<?php
add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
$args=array(
'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>
</div>
</div>
</a>
</div>
<?php
endwhile;
}
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 : )
Surname Name
? Seems like a simpler solution than running terribly slow queries.