开发者

Getting Wordpress posts in a category, ordered by custom field

I am a newbie with WordPress and am trying to modify the standard SQL call for for custom field data posts to limit by category. Here is what I am basing my attempts on, but i just can't figure out how to filter for category 15.

$querystr = "SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'priority'
        AND wposts.post_type = 'post'
    开发者_JAVA百科    AND wposts.post_status = 'publish'
        AND wposts.
    ORDER BY wpostmeta.meta_value ASC";


Use query_posts right before the loop like this :

query_posts('meta_key=priority&order=ASC&orderby=meta_value&cat=15&post_type=post&post_status=publish');

meta_key=priority -> Show posts associated with a certain (priority) custom field.

order=ASC -> self explanatory

orderby=meta_value -> Sort retrieved posts by the meta_value field

cat=15 -> Display posts that have this category (and any children of that category), using category id 15

Aditionaly you could use cat=15,4 to show posts in categoryes 15 and 4 , or category_name=staff,test to show posts in staff and test categories .

More info about using query_posts , usualy you can retrive just about any kind of posts building the query using query_posts . Use plain SQL when you can't use query_posts for whatever reasons .


Read the section Query based on Custom Field and Category in the WordPress Codex article Displaying Posts Using a Custom Select Query. Try googling it next time!

Edit: As poelinca pointed out, consider using query_posts() if you have no highly customized or complex queries to pull off.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜