php - posts order by title second word -
this mutual question, haven't been able find straightforward reply to. want generate list of custom post type titles. im using 'orderby' => 'title'
have them display alphabetically. titles names , lastly names , want sort lastly name. know create meta fields seperated first , lastly names , order lastly name field. i'd see if there no method explode
'title'
, pass sec word in `orderby'.
this basic code:
<?php // '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 alter 'orderby' => $lastname[1],
. doesnt work. there no way utilize sec word in title sort posts?
you can't loop, wordpress doesn't have option.
what can do, it's split title , create array.
$people = array(); while($loop->have_posts()): $loop->the_post(); list($firstname, $lastname) = explode(" ", get_the_title() ); // can utilize list, since said that's first , lastly name on title. $people[] = $lastname; endwhile; wp_reset_query(); sort($people); // sort array asc. note: can create function sort sort or rsort, on input foreach ($people $last) { echo $last, "<br />"; // echo lastly name }
this not efficient, works. should consider adding 2 fields wordpress.
php wordpress
No comments:
Post a Comment