开发者

order by custom field - wordpress

I'm trying to sort a page of posts by a custom field.

Here's what I have so far, I'm just not sure how or where to add the orderby

$args = array(
    'post_type' => 'new',
    'meta_query' => array(
        array(
            'key' => 'over-make',
            'value' => 'Doral',
            'compare' => 'LIKE'
        )
    开发者_如何转开发)

 );
$loop = new WP_Query( $args);


You would use orderby on the same level as post_type and meta_query in your example.

$args = array(
    'orderby' => 'meta_value',
    'post_type' => 'new',
    'meta_query' => array(
        array(
            'key' => 'over-make',
            'value' => 'Doral',
            'compare' => 'LIKE'
        )
    )

 );
$loop = new WP_Query( $args);

(WordPress Codex: WP_Query)


It is probably most suitable to use the get_posts() function:

get_posts('orderby=meta_value_num&meta_key=keyname');

Sources: Get Posts and Interacting with WP Query and Order By Parameters

ps. love the idea of ordering by a meta value, hadn't thought of it before, but it could make several different sorting system easier to build, including a popularity mechanism..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜