How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get Multiple Rows with $wpdb in WordPress
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • 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.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Upload files via Ajax with jQuery using FormData
Web Development

How to Upload Files via Ajax with jQuery

4 Min Read
Laravel request inputs with prefix — filter request()->all() by Str::startsWith
Web Development

How to Retrieve Inputs with a Specific Prefix in Laravel Request

7 Min Read
Install a specific version of a package using Composer (composer require vendor/package:2.1.0)
Web Development

Install a Specific Version of a Package Using Composer (Exact Version + Examples)

5 Min Read
WordPress get current category ID — three methods by page context
Web Development

How to Get the Current Category ID in WordPress

7 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • 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?