How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get Multiple Rows with $wpdb in WordPress
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Get Multiple Rows with $wpdb in WordPress
Web Development

How to Get Multiple Rows with $wpdb in WordPress

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
WordPress wpdb get_results returns multiple rows
SHARE

To get multiple rows from the WordPress database, use $wpdb->get_results() — not get_row(). The get_row() method is designed to return a single row; for an array of records use get_results(), which returns an array of objects (or arrays, with the right output type).

Contents
  • Use get_results() for multiple rows
  • All four $wpdb result helpers
  • Pick the output format
  • Don’t forget prepare()
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on WordPress 6.5. Originally published 2023-05-22, rewritten and updated 2026-05-17.

Use get_results() for multiple rows

global $wpdb;

$start_date = '2026-01-01';
$end_date   = '2026-01-31';

$posts = $wpdb->get_results( $wpdb->prepare(
    "SELECT * FROM {$wpdb->posts}
     WHERE post_date >= %s AND post_date <= %s
     ORDER BY post_date DESC",
    $start_date,
    $end_date
) );

foreach ( $posts as $post ) {
    echo esc_html( $post->post_title ) . '<br>';
}

Two important touches that the source didn’t include: $wpdb->prepare() with %s placeholders for user input (mandatory, prevents SQL injection), and $wpdb->posts instead of a hardcoded wp_posts (works on installs with a custom table prefix).

wpdb result helpers — get_var, get_row, get_col, get_results compared

All four $wpdb result helpers

  • get_var() — single scalar (first column of first row). One post’s title.
  • get_row() — one row as an object. The first matching post.
  • get_col() — one column from every row, flat array. An array of post IDs.
  • get_results() — full row-set as an array of objects. An array of post records.
// One value
$count   = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" );

// One row
$post    = $wpdb->get_row( "SELECT * FROM {$wpdb->posts} WHERE ID = 1" );

// One column
$titles  = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts} LIMIT 10" );

// Full result set
$posts   = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} LIMIT 10" );

Pick the output format

// Default — array of stdClass objects
$posts = $wpdb->get_results( $sql );
echo $posts[0]->post_title;

// Associative arrays
$posts = $wpdb->get_results( $sql, ARRAY_A );
echo $posts[0]['post_title'];

// Numeric arrays
$posts = $wpdb->get_results( $sql, ARRAY_N );
echo $posts[0][3];

// Keyed by the first column (e.g. ID -> row)
$posts = $wpdb->get_results( $sql, OBJECT_K );
echo $posts[42]->post_title;   // row with ID=42

Don’t forget prepare()

// WRONG — direct interpolation, vulnerable to SQL injection
$posts = $wpdb->get_results(
    "SELECT * FROM {$wpdb->posts} WHERE post_status = '$status'"
);

// RIGHT — placeholders with prepare()
$posts = $wpdb->get_results( $wpdb->prepare(
    "SELECT * FROM {$wpdb->posts} WHERE post_status = %s",
    $status
) );

Use %s for strings, %d for integers, %f for floats. The %i placeholder (introduced in WP 6.2) escapes identifiers (table or column names) when you need to interpolate one of those.

Frequently asked questions

What’s the difference between get_row(), get_results(), get_col(), and get_var()?

get_var() returns a single scalar (first column of first row). get_row() returns one row (first matching row). get_col() returns one column from every row as a flat array. get_results() returns the full row-set as an array of objects (or arrays, depending on the output type). Pick the narrowest one — they all share the same query but trim what they return.

Should I always use $wpdb->prepare()?

Yes, anytime the query interpolates user input or any variable that didn’t come from a trusted whitelist. $wpdb->prepare() escapes placeholders for you (%s, %d, %f). Concatenating $_GET or $_POST into a query string is the WordPress version of SQL injection — same vulnerability, same fix.

How do I get associative arrays instead of objects?

Pass the output-type constant as the second argument: $wpdb->get_results($sql, ARRAY_A). Options are OBJECT (default — array of stdClass), OBJECT_K (array keyed by the first column), ARRAY_A (array of associative arrays), and ARRAY_N (array of numerically-indexed arrays).

Why is the table prefix wp_ hardcoded in many examples but not always correct?

WordPress installs can use a custom prefix (set in wp-config.php via $table_prefix). Hardcoding wp_posts breaks on sites that picked a different prefix. Use $wpdb->posts, $wpdb->users, $wpdb->options for core tables — they resolve to the correct prefix. For custom tables, build the name with $wpdb->prefix . 'mytable'.

Related guides

  • How to Prepare a LIKE SQL Statement in WordPress
  • How to Get the Last Inserted Row ID from $wpdb
  • How to Get WordPress Posts by Date Range

References

wpdb class reference: developer.wordpress.org/reference/classes/wpdb. $wpdb->get_results(): developer.wordpress.org/reference/classes/wpdb/get_results. $wpdb->prepare(): developer.wordpress.org/reference/classes/wpdb/prepare.

TAGGED:mysqlphpwordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Get the index in a jQuery each loop How to Get the Index in a jQuery .each() Loop
Next Article Get N elements from a JavaScript array with slice for pagination How to Get N Elements from an Array in JavaScript
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

You Might Also Like

Laravel leftJoin to keep all records even without matches
Web Development

How to Use LEFT JOIN in Laravel to Keep All Records

4 Min Read
Comment in a .gitignore file with the # character
Web Development

How to Comment in a .gitignore File

4 Min Read
jQuery add required attribute to input fields — .prop() method
Web Development

How to Add the Required Attribute to Input Fields with jQuery

5 Min Read
mysqldump all databases — export and import commands
Server Management

How to Export and Import All MySQL Databases at Once

6 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?