• Resolved milanouser

    (@milanouser)


    I have a custom post type (“bb_articles”) with custom fields, among them:
    – “bb_source”: radio button with stored values 1, 2, 3…
    – “bb_date”: date field

    I want to show posts of specific “bb_source” (e.g. 1) ordered by “bb_date”.

    What I get, however, is a order by publication date (it seems), what is NOT what I want.

    My code is:

    $query = array (
                'post_type' => 'bb_articles',
                'meta-key' => 'bb_date',
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                'meta_query' => array(
                      'relation' => 'AND',
                      array('key' => 'bb_source',
                            'value' => 1,
                            'compare' => '='
                            )
                      )
                );
    
                $my_query = new WP_Query($query);

    “bb_date” is stored in DB as raw date, i.e. 4 Nov 2012 is: 1351987200
    thus, ordering by meta_value_num should imho produce the right order.

    Obviously it does not, so where could the error be?

    Any help is greatly appreciated
    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter milanouser

    (@milanouser)

    Edit of previous post:
    Code should be this (without the line 'relation' => 'AND',):

    $query = array (
       'post_type' => 'bb_articles',
       'meta-key' => 'bb_date',
       'orderby' => 'meta_value_num',
       'order' => 'DESC',
       'meta_query' => array(
          array('key' => 'bb_source',
            'value' => 1,
            'compare' => '='
          )
       )
    );
    
    $my_query = new WP_Query($query);

    In addition, I tried a version a way described by Hameedullah Khan on WP stackexchange:

    $query = array (
       'post_type' => 'bb_articles',
       'meta-key' => 'bb_date',
       'orderby' => 'meta_value_num',
       'order' => 'DESC',
       'meta_query' => array(
             array (
                'key' => 'bb_date'
                ),
             array('key' => 'bb_source',
                'value' => 1,
                'compare' => '=='
                )
             )
    );

    Khan says there is a bug: WP modifies the meta_query if you specify orderby and meta_key as query vars. However, his proposed solution (or as I did interpret it) does not solve the problem: my posts are still in the wrong order, i.e. by post publication date rather than by BB_date!

    Again, thank you for any help
    Cheers

    Thread Starter milanouser

    (@milanouser)

    all solved: simple typo: meta_key, not meta-key

    Embarrassing, but true…
    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘query order by date on custom type: wrong order’ is closed to new replies.