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();

1 Answer
1

This may not be the best method, but I was able to achieve my desired result. I ended up setting up a second query.

functions.php

// List staff member by last name
function posts_orderby_lastname ($orderby_statement) {  

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

    return $orderby_statement;
}

staff-grid.php

    echo '<table>';
    echo '<tr>';

    // Query for Directors
    $meta_query = (array(
        'post_type' => 'staff',
        'staff-category' => $the_categories,
        'meta_query'     => array(
            array(
                'key'      => 'director',
                'value'    => 'yes',
                'compare'  => '!='
            ),
        ),
        'orderby'        => 'meta_value',
        'order'          => 'DESC'
    ));
    $meta = new WP_Query( $meta_query );

    // Begin iteration
    $i = 0;  

    if($meta->have_posts()) :

        while($meta->have_posts()) : $meta->the_post();

            $table_cell="<td>$posts</td>";

            echo $table_cell;

            $i++; 

            if($i % 2 == 0) { echo '</tr>'; }

        endwhile; $meta->reset_postdata();

    endif;


    add_filter( 'posts_orderby' , 'posts_orderby_lastname', 10, 2 );
    //Query all staff posts that are not directors
    query_posts (array(
        'post_type' => 'staff',
        'staff-category' => $the_categories,
        'meta_query'     => array(
            array(
                'key'      => 'director',
                'compare'  => 'NOT EXISTS'
            ),

        ),
    ));
    if(have_posts()) :

        while(have_posts()) : 
            the_post();

            $table_cell="<td>$posts</td>";

            echo $table_cell;

            $i++; 

            if($i % 2 == 0) { echo '</tr>'; }

        endwhile; 
        remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );


        $i++; 

        if($i % 2 == 0) { echo '</tr>'; }

    endif;

    echo '</table>';

    wp_reset_query(); //Final reset just in case anything was missed

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *