My first question on the Exchange. I hope I can make this as clear as possible because I don’t know all of the correct terminology. What I’m trying to do is build a staff table. Simple! However, I need for the Director to appear in the first cell and everyone else to appear in alphabetical order by last name. This is a Staff Grid Widget I’m building for the SiteOrigin Page Builder if that helps. I’m also using ACF with a checkbox for the “director”. The code below is what I have so far. Thank you in advance for your help.
Current result: I’m getting the table and the director is first but the staff isn’t in alpha order
Expected result: Director “m” | Staff “a” | Staff “b” | Staff “c” | … | Staff “m” | Staff “n” | …
functions.php
// List staff member by last name
function posts_orderby_lastname ($orderby_statement, $query) {
global $wpdb;
//Used var_dump to view meta data to check for
$query = get_post_meta(get_the_ID(), 'director', true);
if ($query == '') {
$orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
} else {
$orderby_statement = ""
}
return $orderby_statement;
}
staff-grid.php
add_filter( 'posts_orderby' , 'posts_orderby_lastname', 10, 2 );
//Query all staff taxonomy posts
$staff_args = array(
'post_type' => 'staff',
'staff-category' => $the_categories,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'director',
'value' => 'yes',
'compare' => '!=',
),
array(
'key' => 'director',
'value' => '',
'compare' => 'NOT EXISTS',
),
),
'orderby' => 'meta_value',
'order' => 'DESC',
);
$query = new WP_Query( $staff_args );
// Begin iteration
$i = 0;
if ($query->have_posts()):
<table>
<tr>
while($query->have_posts()) : $query->the_post();
<td> $post </td>
$i++;
if($i % 2 == 0) {
echo '</tr>';
}
endwhile;
echo '</table>';
remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );
endif;
wp_reset_query();