WordPress select statement with order by

I have a wordpress query which works fine without the order by, however when i add it in, it returns nothing.

 public function getUsers(){

    global $wpdb; 

    return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}users ORDER BY $wpdb->con_created_at ASC", OBJECT );

}

Can anyone see where im going wrong? Cheers

Related posts

1 comment

  1. You can’t get value of object property in quotes like this. You are getting prefix property value correctly. Get con_created_at same way.

    return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}users ORDER BY {$wpdb->con_created_at} ASC", OBJECT );
    

    or use string concatenation

    return $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."users ORDER BY ".$wpdb->con_created_at." ASC", OBJECT );
    

    But I think $wpdb dosen’t have con_created_at. This is just field name. So

    return $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."users ORDER BY con_created_at ASC", OBJECT );
    

    should be correct.

    For more info visit PHP manual.

Comments are closed.