Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The user starts at a page showing posts of custom post type, "agent".

Each post displayed shows a region/custom taxonomy it belongs to and links to a page that shows all posts in the region the user clicked.

This is how these posts are currently displayed,

<?php while ( have_posts() ) : the_post(); ?>

Ideally I'd like to to sort these alphabetically by meta_key lname rather than by post date which appears to be the wordpress default.

Thanks for any input

Edit: Script I use to get all custom post type Agents. Puts one specified post as last post.

<?php 
    $loop = new WP_Query( array( 
        'post_type' => 'agents',
        'orderby' => 'meta_value',
        'meta_key' => 'lname',
        'order'=>'ASC',
        'meta_query' => array(array( 'key' => 'lname' )),
        'post__not_in' => array( '93' ),
        'posts_per_page' => -1 ) );
    $loop2 = new WP_Query( array( 
        'post_type' => 'agents',
        'post__in' => array( '93' ) ) );
?>
<?php
    if($loop->have_posts() || $loop2->have_posts()) { 
    if($loop->have_posts()) { while($loop->have_posts()) { $loop->the_post();
?>
share|improve this question
add comment

3 Answers

Maybe try something like this before the loop:

<?php
query_posts( wp_parse_args( $query_string, array(
    'meta_key' => 'your_meta_key',
    'orderby' => 'meta_value'
    ) ) );
?>
share|improve this answer
    
when I try your code it doesn't show any posts anymore –  Chuck Jun 23 '11 at 2:44
add comment

Pretty much the same as the above to answers, but using meta_query instead of meta_key. meta_key was deprecated as of WP 3.1. I'm not sure if ordering by meta_value still works in this case, however.

$wpse20766args = array(
     'orderby'  => 'meta_value',
     'order'    => 'ASC',
     'post_type'    => 'agent',
     'posts_per_page' => -1,
     'meta_query'   => array(
        array( 
            'key' => 'lname'
        )
     ) 
);

$wpse20766q = new WP_Query( $wpse20766args );

// then the loop...
if( $wpse20766q->have_posts() ):
while( $wpse20766q->have_posts() ) :
$wpse20766q->the_post(); 

// do stuff...

endwhile;
endif;
share|improve this answer
    
Well, it sorts now, but it doesn't appear to affect posts by admins, they are at the top, and it isn't showing just the region/taxonomy that I click, it shows all regions/taxonomies. –  Chuck Jun 23 '11 at 3:12
    
You're going to have to do a similar thing to create pages for taxonomies. you'll need a taxonomy-your_custom_tax_name.php file, then sort those by meta_value. What do you mean it doesn't effect posts by admins? Is their lname field set to 'admin'? –  chrisguitarguy Jun 23 '11 at 3:33
    
In my previous comment I was using the code you posted above in my taxonomy-regions.php. Not sure about the admin issue, my agents custom post type has a meta field for lname, I use nearly identical code as what you posted for the page which lists all agents and it doesn't seperate posts of admins from authors, see edit above for this code I use to search for all agents posts. –  Chuck Jun 23 '11 at 3:48
    
Get rid of admins by adding an 'author' => -1 (replace -1 with -[your admins user ID]) to the query. –  chrisguitarguy Jun 23 '11 at 12:33
add comment
up vote 0 down vote accepted

This is what ended up working.

global $wp_query;   

query_posts(array_merge(array('orderby' => 'meta_value', 'meta_key' => 'rw_lname' ),$wp_query->query));

<?php while ( have_posts() ) : the_post(); ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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