0

I'm try to get the output of this post type to order by the staff name alphabetically and I can't seem to figure out if needs to be within the beginning array() or if I been do something within the foreach() loop.

Here's my constructed loop:

 <?php
 $mypost = array('post_type' => 'staff', 'posts_per_page' => 20, 'orderby' => 'staff-name',
    'order' => 'ASC' );
 $loop = new WP_Query( $mypost );

 ?>
<ul>

<?php while ( $loop->have_posts() ) : $loop->the_post();?>

 <?php $items = get_post_meta( $post->ID, 'elements', true ); 
 foreach( $items as $i){

echo'<li>
    <a href=" '. get_permalink() .' ">' . $i['staff-name'] . '</a>
    </li>
    ';
}
?>  

<?php endwhile; wp_reset_query(); ?>

The staff-name field hold the first and last name of the staff member, but for some reason it doesn't look like it's sorting them alphabetically correctly.

I do notice that if I change it from ASC to DESC it does change the order of the output, but it's not alphabetized. It's almost like it's ignoring the field staff-name and ordering it by the post ID instead.

2
  • 1
    possible duplicate of Orderby custom field for custome post type
    – s_ha_dum
    Commented Nov 25, 2013 at 21:17
  • I saw that and I tried to mimic the answer, but it just comes back with an empty result. This is my updated part of the array: $mypost = array('post_type' => 'staff', 'posts_per_page' => 20, 'meta_key' => 'staff-name', 'orderby' => 'staff-name', 'order' => 'ASC' ); Commented Nov 25, 2013 at 21:34

2 Answers 2

0

You have to add:

'meta_key' => 'key_name'

to the arguments array in order to use the key with the orderby parameter. Additionally you have to have the orderby parameter like this:

'orderby' => 'meta_value'

But if you have the data you want to sort by saved into an array, like your code suggests, you have a problem. You won't be able to use it for sorting, take a look at the answer to this question WP_Query meta_query by array key for an explanation.

6
  • I added this: $mypost = array('post_type' => 'staff', 'posts_per_page' => 20, 'meta_key' => 'staff-name', 'orderby' => 'staff-name', 'order' => 'ASC' ); And it returns nothing. Just comes back blank on the page. Am I missing something else? Commented Nov 25, 2013 at 21:33
  • Ah, ok. Yeah, I've updated the array to this: $mypost = array('post_type' => 'staff', 'posts_per_page' => 30, 'meta_key' => 'staff-name', 'orderby' => 'meta_value', 'order' => 'ASC' ); And it just returns an empty result. Bleh. Well, I guess I could re-structure my data sets within the custom post types and custom fields and then separate the name into first name and last name and then use PHP to sort out the array that way. Might just have to go to with the re-order plugin and see if that helps since I'm running out of time with this project. But thanks for your input! Commented Nov 25, 2013 at 22:23
  • Like I said and it's explained at the linked source, restructuring is your best shot, if you want to do it this way, everything else is really just a painful hack. Good luck! @ultraloveninja I've see you have some answered questions you haven't resolved by check marking, can you do this please. Commented Nov 25, 2013 at 22:37
  • Thanks! Yeah, I just popped in the Post Types Order plugin and that seemed to do the trick. While I really wanted to just leverage wordpress (or PHP) to do the sorting, I've run out of time with this project and that was the best solution for me. But I do appreciate the help and I will mark the answer checked on the resolved answers. Commented Nov 25, 2013 at 22:43
  • Just a fair warning, the »post types order« plugin uses menu_order for ordering, this can get painful, if you are dealing with lots of posts...now you can't say you haven't been warned :D @ultraloveninja Commented Nov 25, 2013 at 22:48
0

If your meta_key is 'staff-name', you don't want 'orderby' => 'staff-name', you want 'orderby=>'meta_value'

$mypost = array(
  'post_type' => 'staff', 
  'posts_per_page' => 20, 
  'meta_key' => 'staff-name', 
  'orderby' => 'meta_value', 
  'order' => 'ASC' 
);

This is the essence of the duplicate question and is explained in the Codex as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.