0
votes

Below is the code which I have written to WP_Query a bunch of filters which involves post's custom meta fields,

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

What I want to achieve from the above query is, " select this month's published posts from 'read' category with meta key 'city' having value 'NY' and order by 'meta_value_num' of meta_key 'article_order' "

1
Put city in quotes 'city'. And also tell us what is not working ? Do you have such post ? Does it not fetching it ? - Rikesh
@Rikesh $args = array('numberposts' => 100, 'category'=>2, 'post_status' => "publish", 'order'=>"ASC", "orderby" => "meta_value_num", "meta_key" => "article_order"); //is working $posts = get_posts( $args ); //when i put get_posts. <br /> This code is working. When I put 'meta_key'=>"city", 'meta_value'=>'NY', and 'cat'=>"read", "monthnum" => $month to WP_Query Its not working. - Nagendra Rao

1 Answers

0
votes

may be try some thing like

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

hope this work and also in your code you are missing '' in your 'meta_key'=>city as 'meta_key'=>'city'

hope this work for you