[resolved] Group Posts by Custom Field - Alphabetically (15 posts)

  1. JTWilcox
    Posted 5 years ago #

    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.

  2. aphill70
    Posted 5 years ago #

    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.


  3. JTWilcox
    Posted 5 years ago #

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

  4. aphill70
    Posted 5 years ago #

    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.

  5. JTWilcox
    Posted 5 years ago #

    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!

  6. aphill70
    Posted 5 years ago #

    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.

  7. JTWilcox
    Posted 5 years ago #

    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.

  8. aphill70
    Posted 5 years ago #

    the error should be giving you a line number right?

  9. aphill70
    Posted 5 years ago #

    <?php $prevltr = ""

    at this line in the original code delete the


  10. JTWilcox
    Posted 5 years ago #

    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.

  11. aphill70
    Posted 5 years ago #

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

  12. JTWilcox
    Posted 5 years ago #

    Here's where I'm at right now:


  13. JTWilcox
    Posted 5 years ago #

    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:


  14. aphill70
    Posted 5 years ago #

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

  15. alturnwall
    Posted 4 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic