0

I have this meta_query , which is:

A post type " newsletter " with a custom field I created called " date_newsletter " which would make the records of a year and month given specific and sort both by the " date_newsletter " field and also sort by the id of the post .

Meta_query but this did not work for me .

Any help?

$args = array(
        'post_type' => 'newsletter',
        'meta_query' => array( 
            array(
                'key' => 'date_newsletter', 
                'value' => date("Y"), 
                'compare' => '=', 
                'value' => $y, 
            ),
            array(
                'key' => 'date_newsletter', 
                'value' => date("m"), 
                'compare' => '=', 
                'value' => $m, 
            )
        ),
        'meta_key' => 'date_newsletter',
        'orderby' => 'meta_value',
        'orderby' => 'id',
        'order' => 'DESC',
        'showposts' => '-1'

1 Answers1

0

you have value set twice. You should order by meta_value_num, also it should be posts_per_page instead of showposts (updated in 2.1). Your orderby should be in an array - not separate.

your new code:

$args = array( 
                                'post_type' => 'newsletter', 
                                'post_status' => 'publish', 
                                'meta_key' => 'date_newsletter', 
                                'meta_query' => array(
                                                    array(
                                                    'key' => 'date_newsletter', 
                                                    'value' => date("Y"), 
                                                    'compare' => '=',
                                                    ),
                                                array(
                                                    'key' => 'date_newsletter', 
                                                    'value' => date("m"), 
                                                    'compare' => '=',
                                                    )
                                                ),
                                'orderby'=> array('meta_value_num', 'ID'), 
                                'order' => 'DESC', 
                                'posts_per_page' => -1);
Moishy
  • 3,560
  • 3
  • 23
  • 42