Support » Fixing WordPress » Group Posts by Custom Field – Alphabetically

  • Resolved JTWilcox


    Hello everyone,

    I’m piecing together a custom employee directory using a custom post type called “professionals” and a taxonomy to group them called “titles”. I’ve been organizing everything based on a custom field for each employee’s last name (custom field key is ‘prof_lastname’).

    I’d like to list all employees alphabetically, grouped underneath each letter, based on that key. I found some code here that allows you to do it based on post title, and am just wondering if there’s a way to modify this to use a custom field instead.

    More specifically, here’s the section I’m trying to work with as it currently exists on my site:

    			  $querystr = "
    			  SELECT * FROM $wpdb->posts
    			  LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    			  LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    			  LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    			  WHERE $wpdb->posts.post_type = 'professionals'
    			  AND $wpdb->term_taxonomy.taxonomy = 'titles'
    			  AND $wpdb->posts.post_status = 'publish'
    			  AND $wpdb->postmeta.meta_key = 'prof_lastname'
    			  ORDER BY $wpdb->postmeta.meta_value ASC
    			   $pageposts = $wpdb->get_results($querystr, OBJECT);
    			 <?php if ($pageposts): ?>
    			  <?php while ($my_query->have_posts()) : $my_query->the_post();
                        $this_char = strtoupper(mb_substr($post->post_title,0,1,'UTF-8'));
                        if ($this_char != $last_char) {
                          $last_char = $this_char;
                          echo '<a name="section-'.$last_char.'"></a><br class="clear" /><h2>'.$last_char.'</h2>';
                        } ?>
                      <article class="professional-entry" id="entry-<?php echo $last_char ?>">
                          <div><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong></div>
                          <div><?php $terms = get_the_terms( $post->ID , 'titles' ); foreach( $terms as $term ) { print $term->name; unset($term); } ?></div>
                          <div><a href="mailto:<?php echo get_post_meta($post->ID, 'prof_email', true); ?>"><?php echo get_post_meta($post->ID, 'prof_email', true); ?></a></div>
                  <?php endwhile; ?>
    			<?php endif; ?>

    I’m querying posts within my custom post type, getting the meta_key “prof_lastname” for each entry, shortening it down to the first letter and displaying various things based off of that. The code to group them by letter is still doing it by post title (so right now that means alphabetically by first name) but I’d like it organized by that meta_key (by last name).

    Let me know if this is too confusing. I’ve been going nuts with it myself. Thanks in advance.

Viewing 14 replies - 1 through 14 (of 14 total)
  • you can just use the wordpress wpQuery class to do this rather than querying the database directly.

    $employeeQuery = new WP_Query(array('post_type' => '<your_post_type>', 'meta_key' => '<your_custom_field_key>', 'orderby' => 'meta_value', 'order' => 'ASC'))

    Then you can just run that through “the Loop”.

    This is the page about the WP_Query class.

    Thanks, that is a lot easier than the query I was doing. The problem I’m having with that code, though, is that it ungroups everything. Maybe I should have explained this a bit better before, but I’m wanting the page to group and display posts like this:


    Peter Alvin
    James Archer


    Tom Bombadil


    I guess I just don’t know how to plug that info into my loop to modify what I had. Particularly the line

    $this_char = strtoupper(mb_substr($post->post_title,0,1,'UTF-8'));

    the $post variable is comp that code should work just fine within the loop using the other code.

    the $post variable is created by “The Loop”

    the code you have above should work just fine with my query.

    What i am not following is why you are getting the first char of the post_title and not your prof_lastname first char.

    that could be the problem with your grouping is that its looking at whatever is in the post title not the lastname field…

    correct me if i am wrong my code reading is far from perfect.

    No you’re right. I’m just saying I don’t know exactly what to put into that line to change it FROM post title TO prof_lastname. I’m just cobbling together various code I’ve found and don’t know the precise way to put them all together. Needless to say I really appreciate the help!

    ok i think this should work…

    i put it here that should spit out all the last names if i made all the right assumptions about your setup.

    hopefully i don’t have any huge syntax errors since i can’t run it.

    here is the link for the function i used to get the last name

    if you have any questions let me know.

    aphill, I am getting some syntax errors on this one unfortunately. I found a couple instances of, I think, unclosed PHP tags. After fixing those, though, I’m still getting some syntax errors. Like an unexpected ‘;’ Any ideas? Thanks.

    the error should be giving you a line number right?

    <?php $prevltr = ""

    at this line in the original code delete the


    Yeah I just keep getting all sorts of syntax errors. Sometimes it just says something like “unexpected ‘?’ on line…” but I don’t know what it’s talking about. There’s no question mark.

    If you post your code using I will take a look at it.

    Here’s where I’m at right now:

    Hey aphill, I got it! I worked through those errors I was getting, spruced up the code very slightly, and now it’s working perfectly! Thanks so much for all of your help.

    For reference, here’s my final code for this puzzle:

    Glad to hear it sorry i couldn’t get to it this weekend

    Wow, I was trying to do the same thing and that code worked wonderfully. Thanks for sharing—you saved me a day’s worth of Googling 🙂

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Group Posts by Custom Field – Alphabetically’ is closed to new replies.