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 have a custom post type (CPT) called property. I have registered a featured property metaboxfor it as explained in this question "How do I create a featured post within a custom post type?". So a featured property has got meta_key='property_featured' and meta_value=on.

What I would like to do is to display all property posts, but order them by meta_key='property_featured'. So that featured properties will appear first in the list on the first page. Similar behavior as Sticky Posts functionality. And all the rest property posts will be ordered by date created. Also, I need to make sure that pagination is working correctly - treating all the property posts alltogether. I use custom pagination code explained here. (Hope that makes sense).

I have tried to specify arguments for WP_Query. However, if I specify:

'meta_key' => 'property_featured',
'orderby' => 'meta_value'

then only property posts with that key are displayed as opposite to all property posts.

If I delete meta_key from arguments then the query doesn't know what to sort them by.

How do I display all the property posts, making sure that featured ones appear first and all other property posts order by published date?

Many thanks, Dasha

share|improve this question
    
possible duplicate of Order by meta value or date? –  Jan Fabry May 10 '11 at 15:06

2 Answers 2

up vote 1 down vote accepted
<?php
// query posts
$query_property = query_posts( array( 
     'orderby'    => 'date meta_value' // orderby date AND meta value
) );

// First loop
$query_feat = $query_string.'&meta_value=on&meta_key=property_featured';
// Offset for second loop
$query_all = $query_string.'&offset=3&meta_key=property_featured';

// First loop
if ( $query_feat->have_posts() : while ( $query_feat->have_posts() ) : # etc....
    // do stuff
endif;

// Second loop
if ( $query_all->have_posts() : while ( $query_all->have_posts() ) : # etc....
    // do stuff
endif;
?>
share|improve this answer
    
it's a 'basic' approach - you should anyway safe another value for that meta key (ex. "off") on save_post and query the second posts by this value. –  kaiser May 2 '11 at 14:55
1  
thanks for the reply. You're right I ended up saving property_featured meta key with the default value of "off", otherwise they weren't even considered by SQL. –  dashaluna May 18 '11 at 11:56

I think what you are trying to do her is a little too complex. Essentially you want two (meta and date) sort orders applied. Also I think that order by meta value will not work for posts that don't have that value at all.

Stickes is accurate comparison, but note that they are not handled by SQL request, but by completely separate code - they are re-ordered, fetched and applied to results of already completed query.

Unless you want to fork and rewrite stickes logic I think the easiest way would be to move your featured posts to separate loop.

share|improve this answer

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.