开发者

PHP Query posts and add unqueried posts to end

Okay, so through Wordpress, I've utimately ended up with a query string of the following:

"SELECT * FROM wp_z4azbl_posts 
LEFT JOIN wp_z4azbl_postmeta ON(
    wp_z4azbl_posts.ID = wp_z4azbl_postmeta.post_id) 
LEFT JOIN wp_z4azbl_term_relationships ON(
    wp_z4azbl_posts.ID = wp_z4azbl_term_relationships.object_id) 
LEFT JOIN wp_z4azbl_term_taxonomy ON(
    wp_z4azbl_term_relationships.term_taxonomy_id = wp_z4azbl_term_taxonomy.term_taxonomy_id) 
LEFT JOIN wp_z4azbl_terms ON(
    wp_z4azbl_terms.term_id = wp_z4azbl_term_taxonomy.term_id) 
WHERE wp_z4azbl_开发者_高级运维term_taxonomy.taxonomy = 'opportunitytype' 
AND wp_z4azbl_posts.post_status = 'publish' 
AND wp_z4azbl_posts.post_type = 'opportunities' 
AND wp_z4azbl_postmeta.meta_key = 'date_timestamp' 
GROUP BY wp_z4azbl_posts.ID 
ORDER BY wp_z4azbl_postmeta.meta_value ASC"

Ultimately this works. It grabs my posts with a timestamp custom field and sorts it in ascending order. However, I need it to also append posts that don't have a timestamp after all the posts that have a timestamp.

So right now, it showing something like this:

Event Title
January 1, 2011

Event Title 2
January 2, 2011

But I want it to append all posts without a timestamp:

Event Title
January 1, 2011

Event Title 2
January 2, 2011

Event Title Nostamp

Event Title Nostamp 2

How would I alter my query to get this to work?

EDIT

Sorry, I messed up on my original query. I actually need them to sort in ascending order with the non-timestamp posts still at the bottom.


To include rows without a timestamp in the resultset, move meta_key = 'date_timestamp' from the where clause to on:

LEFT JOIN 
        wp_z4azbl_postmeta 
ON      wp_z4azbl_posts.ID = wp_z4azbl_postmeta.post_id
        and wp_z4azbl_postmeta.meta_key = 'date_timestamp'

Since you are sorting descending, the null values will already appear on the bottom. null is treated as the lowest possible value for purposes of sorting.

EDIT: To sort null differently, use a case:

order by
        case
        when wp_z4azbl_postmeta.meta_value is null then 2
        else 1
        end
,       wp_z4azbl_postmeta.meta_value
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜