Hi everyone,
I’m trying to get an array of records from a WordPress database using $wpdb->get_row()
, but it only returns one row. How can I get multiple rows?
Hi everyone,
I’m trying to get an array of records from a WordPress database using $wpdb->get_row()
, but it only returns one row. How can I get multiple rows?
The reason why $wpdb->get_row() only returns one row is because it is designed to do so. It is useful when you only need one record from the database, such as a single post or a user profile.
However, if you want multiple rows, you need to use $wpdb->get_results() instead. This function returns an array of objects or arrays, depending on your specific output type.
For example, if you want to get all the posts that were created between a certain date range, you can use this code:
$start_date = '2023-01-01';
$end_date = '2023-01-31';
$posts = $wpdb->get_results("SELECT * FROM wp_posts WHERE created >= '$start_date' and created <= '$end_date' ORDER BY created DESC");
This will return an array of post objects you can loop through and display on your page.
I hope this helps.