$wpdb->get_row() Only Returns a Single Row

If you are trying to get an array of records that match a select statement using wpdb->get_row(), I’m afraid you are going to be disappointed. $wpdb->get_row() only returns a single row when you are using it to retrieve records from a WordPress database.

Return Multiple Rows with $wpdb->get_results()

However, if you would like to retrieve an array of records use the following code instead:

$wpdb->get_results()

For example, the following code, using the $wpdb->get_row function, will retrieve a single row, or, in other words, a single instance of the post object:

$post = $wpdb->get_row("SELECT * FROM wp_posts WHERE created >= '$start_date' and created <= '$end_date' ORDER BY created DESC");

However, if you would like to retrieve multiple rows, or an array of instances of the post object, you would need to use the $wpdb->get_results function. See the code below as an example:

$posts = $wpdb->get_results("SELECT * FROM wp_posts WHERE created >= '$start_date' and created <= '$end_date' ORDER BY created DESC");

Leave a Reply

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