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.

I would like to display all agents a-z, except there is one agent who should always show up last. Ideally I'd like the ordering to be done from the value of a custom field, dName.

I was looking around it seemed that meta_query was the new best way to do this but haven't figured it out yet.

Current code that shows 10 last agents created.

<?php $loop = new WP_Query( array( 'post_type' => 'agents', 'posts_per_page' => 10 ) ); ?>

Thanks for looking.

edit 3/27/2011, working code

<?php 
$loop = new WP_Query( array( 
    'post_type' => 'agents',
    'orderby' => 'meta_value',
'meta_key' => 'rw_dname',
    'order'=>'ASC',
    'meta_query' => array(
        array( 'key' => 'rw_dName' )
        ),
    'post__not_in' => array( '98' ),
    'posts_per_page' => -1 ) );

$loop2 = new WP_Query( array( 
    'post_type' => 'agents',
    'post__in' => array( '98' ) ) );?>
<?php
if($loop->have_posts() || $loop2->have_posts()) { 
    if($loop->have_posts()) { while($loop->have_posts()) { $loop->the_post();?>
<div class="agentContent">
    <div class="agentThumbnail">
        <?php 
        if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail();
}?>
</div>
<div class="agentInfo"> <span class="agentInfoEntry">
        <?php
$meta = get_post_meta(get_the_ID(), 'rw_dname', true);
echo $meta; // if you want to show
?>
        </span> </div>
</div>
<?php
        } //endwhile
    }
?>
<?php
if($loop2->have_posts()) { while($loop2->have_posts()) { $loop2->the_post();?>
<div class="agentContent">
    <div class="agentThumbnail">
    <?php 
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
} 
?>
</div>
    <div class="agentInfo"> <span class="agentInfoEntry">
    <?php
$meta = get_post_meta(get_the_ID(), 'rw_dname', true);
echo $meta; // if you want to show
?>
        </span> </div>
</div>
<?php
        } //endwhile
    }
?>
<?php
} else {
    echo 'No Agents.';
}
?>
share|improve this question
    
What's the value of the field dName for the one you want to show up last? What should happens that agent it is not in the natural order of the 10; you still want it included? –  MikeSchinkel Mar 23 '11 at 1:09
    
the value of the field dName is "firstname lastname" without the quotes. I want to show all the agents not just the 10, the code above was just to show what I was currently using to show the agents. –  Chuck Mar 23 '11 at 1:28
    
Is the agent that shows last using accented characters in his/her name? –  t31os Mar 23 '11 at 11:04
    
nope, just a-z and A-Z –  Chuck Mar 23 '11 at 20:51
add comment

1 Answer

up vote 0 down vote accepted

I just wrote this up so I have not tested it but this is how I would go about forcing one post to be at the end. In the first loop it excludes the posts by its ID number and in the second loop it only includes the post by the ID number, essentially you will need to know the ID of the post.

<?php 
$loop = new WP_Query( array( 
    'post_type' => 'agents',
    'orderby' => 'meta_value',
    'meta_key' => 'dName',
    'post__not_in' => array( 00 ),
    'posts_per_page' => -1 ) );
$loop2 = new WP_Query( array( 
    'post_type' => 'agents',
    'post__in' => array( 00 ) ) );

if($loop->have_posts() || $loop2->have_posts()) { 
    if($loop->have_posts()) { while($loop->have_posts()) { $loop->the_post();
        echo the_title();
        echo get_post_custom_values('dName');
        //Post Content
        } //endwhile
    }
    if($loop2->have_posts()) { while($loop2->have_posts()) { $loop2->the_post();
        echo the_title();
        echo get_post_custom_values('dName');
        //Post Content
        } //endwhile
    }
} else {
    echo 'No Agents.';
}
?>
share|improve this answer
    
I guess you missed the both endif; curly brackets for the loops. On the other side: Why are you questioning 2 times if $loop->have_posts()? –  kaiser Mar 23 '11 at 2:59
    
There's two loops there, look closely. –  t31os Mar 23 '11 at 11:04
    
@imHavoc ok I finally had some time this evening to take a look at this, I understand the logic of what is going on, but the closest I've gotten so far is "no agents" –  Chuck Mar 25 '11 at 1:03
    
and the rest of my comment. I edited my original question with the current pages code, which results in this, "Parse error: syntax error, unexpected ';' in ... on line 27" The closest I've gotten to anything showing up is "NoAgents" after commenting out all parts of loop2. Thanks again for any help. –  Chuck Mar 25 '11 at 1:16
    
@Chuck - I missed the closing parenthesis in loop2. To fix the Parse error add another parenthesis to the ends so it should look like this 'post__in' => array( 00 ) ) );. As for why the loops is not working, I will need to look into that. –  imHavoc Mar 26 '11 at 19:27
show 2 more comments

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.