As title implies, I cannot seem to get $wpdb->get_results to give results. Running the “raw” query is successful, but this seems to be failing for some strange unknown reason.

    global $wpdb;


    $sql = "SELECT *
            FROM something aki, 
                 somethingelse akb, 
                 somethingelseelse ac 
            WHERE aki.keyID = akb.keyID 
              AND akb.someID = ac.someID 
              AND aki.type LIKE '%thing%'";

    $corps = $wpdb->get_results($sql, OBJECT);

    var_dump($corps);

4 s
4

Use $wpdb->show_errors( true ) before the query and see what error comes back.

My problem (because I’ve experienced the same thing) was that I should use $wpdb->posts instead of wp_posts inside the query. The prefix of the tables can change from WP installation to installation or even in the same installation depending on the time (e.g. an admin can change the prefix whenever he likes). So, one should write the query like that:

$query = "select <stuff here> from $wpdb->posts where <stuff here>";

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *