This is a common question, i haven’t been able to find a straightforward answer to.
I want to generate a list of a custom post type titles. im using 'orderby' => 'title'
to have them display alphabetically. The titles are names and last names and i want to sort by the last name. I know i could create meta fields with seperated first and last names and order by the last name field. But i’d really like to see if there is no good method to explode
the 'title'
and pass the second word in to `orderby’.
This is the basic code:
<?php
// Get the 'Actors' post type
$args = array(
'post_type' => 'actors',
'orderby' => 'title',
'order' => 'ASC',
'nopaging' => true,
'cache_results' => false,
'update_post_meta_cache' => false
);
$loop = new WP_Query($args);
while($loop->have_posts()): $loop->the_post();
echo '<li><img src="' . get_post_meta(get_the_ID(),'_dtm_small_image', [0]) . '"><span>' . get_the_title() . '</span></li>';
endwhile;
wp_reset_query();
?>
Iv’e tried to:
$gettitle = get_the_title();
$lastname = explode(" ", $gettitle);
and then change to 'orderby' => $lastname[1],
. but it doesnt work.
Is there no GOOD way to use the second word in the title to sort posts?