WordPress.org

Support

Support » How-To and Troubleshooting » Query & Order posts by custom fields

Query & Order posts by custom fields

  • Hey guys!

    I’m trying to query a custom post type “jogos” where the meta value “data_de_lancamento” (launch date) is <= than today’s.
    That field uses the format dd/mm/yy.

    The returned posts from that query must then be ordered Descending by the custom field “views”.

    My current code is:

    date_default_timezone_set('Europe/Lisbon');
    $data = date("d/m/y");
    global $wpdb;
    
    $sql = "SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2
    WHERE wposts.ID = wpostmeta.post_id
    AND wposts.ID = wpostmeta2.post_id
    AND wpostmeta.meta_key = 'views'
    AND wpostmeta2.meta_key = 'data_de_lancamento'
    AND wpostmeta2.meta_value < $data
    AND wposts.post_type = 'jogos'
    AND wposts.post_status = 'publish'
    ORDER BY wpostmeta.meta_value ASC";
    
    $resultado = $wpdb->get_results($sql, OBJECT);

    The current problem with that code is that nothing is returned…

    Any help would be appreciated 🙂

    Thank you

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Query & Order posts by custom fields’ is closed to new replies.
Skip to toolbar