WordPress.org

Ready to get started?Download WordPress

Forums

Query & Order posts by custom fields (2 posts)

  1. Tiago Duarte
    Member
    Posted 3 years ago #

    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

  2. Tiago Duarte
    Member
    Posted 3 years ago #

    Bump? No replies and the topic is going to the void... :(

Topic Closed

This topic has been closed to new replies.

About this Topic