【问题标题】:WP_Query which includes custom meta fields of a postWP_Query,其中包括帖子的自定义元字段
【发布时间】:2013-03-19 23:38:30
【问题描述】:

下面是我写给 WP_Query 的代码,其中包含了一些涉及帖子自定义元字段的过滤器,

        <?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 );
        ?>

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

【问题讨论】:

  • city 放在引号'city' 中。并告诉我们什么不起作用?你有这样的帖子吗?它没有获取它吗?
  • @Rikesh $args = array('numberposts' => 100, 'category'=>2, 'post_status' => "publish", 'order'=>"ASC", "orderby" => "meta_value_num", "meta_key" => "article_order"); //正在工作 $posts = get_posts( $args ); //当我放 get_posts.
    此代码正在运行。当我将 'meta_key'=>"city"、'meta_value'=>'NY' 和 'cat'=>"read"、"monthnum" => $month 放入 WP_Query 时,它不起作用。

标签: php wordpress wordpress-theming


【解决方案1】:

可以试试

<?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'=&gt;city 中缺少'' 作为'meta_key'=&gt;'city'

希望这对你有用

【讨论】:

  • 谢谢@anstrangel0ver 我还没有尝试过。如果它有效,那么我会回来接受你的回答。
  • 你肯定是我刚刚阅读了你上面的评论,如果我的代码不工作可能会很有趣
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多