WP_Query,其中包含帖子的自定义元字段

时间:2013-03-29 09:12:30

标签: php wordpress-plugin wordpress-theming wordpress

下面是我写给WP_Query的一堆代码,其中包含post的自定义元字段,

        <?php
            global $post;
            $month=date("m", time());    //current month
            $month=intval($month);       //converting '03' to int 3
            $args = array( 'meta_key'=>"city", 'meta_value'=>'NY',   'numberposts' => 100, 
'cat'=>"read", "monthnum" => $month, 'post_status' =>  "publish", 
'order'=>"ASC", "orderby" => "meta_value_num", "meta_key" => "article_order");      
            $posts = WP_Query( $args );
        ?>

我想从上述查询中实现的是,“从'读'类别中选择本月发布的帖子,其中元键'city'的值为'NY',并按'meta_value_num'排序meta_key'article_order'”

1 个答案:

答案 0 :(得分:0)

可能会尝试像

这样的事情
<?php
            global $post;
            $month=date("m", time());    //current month
            $month=intval($month);       //converting '03' to int 3
            $args = array( 'meta_query' => array(
        array(
            'key' => 'city',
            'compare' => '==',
            'value' => 'NY',
        )
    ), 'numberposts' => 100, 
'cat'=>"read", "monthnum" => $month, 'post_status' =>  "publish", 
'order'=>"ASC", "orderby" => "meta_value_num", "meta_key" => "article_order");      
            $posts = WP_Query( $args );
        ?>

希望这项工作以及您的代码中缺少''meta_key'=>city 'meta_key'=>'city'

希望这对你有用