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 Prepare a %LIKE% SQL Statement 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 Prepare a %LIKE% SQL Statement in WordPress
Web Development

How to Prepare a %LIKE% SQL Statement in WordPress

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
WordPress prepare LIKE SQL — %s placeholder + % wildcards in the value
SHARE

The wordpress prepare like pattern is always the same: put the placeholder %s in the SQL string, put the literal % wildcards around the value when you pass it in. The common wrong attempt — writing LIKE '%%s%' inside the query and passing just $text — confuses prepare()‘s printf-style formatter and breaks. This guide shows the correct shape, the esc_like() step that keeps user-supplied metacharacters from leaking semantics, and when to skip $wpdb entirely for WordPress’s built-in query classes.

Contents
  • TL;DR
  • Why the common attempt breaks
  • The correct shape
  • Add esc_like() for user input
  • Common patterns
  • When to skip $wpdb
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on WordPress 6.5 and PHP 8.3. Originally published 2022-10-07, rewritten and updated 2026-04-23.

TL;DR

$sql = $wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}my_table WHERE column LIKE %s",
    '%' . $wpdb->esc_like( $text ) . '%'
);

$rows = $wpdb->get_results( $sql );

Why the common attempt breaks

First instinct is to keep the wildcards in the query string:

// Broken
$wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}my_table WHERE column LIKE '%%s%'",
    $text
);

prepare() formats the query through a printf-style routine, where %% means a literal % and %s is the string placeholder. So %%s% parses as “literal percent, placeholder, orphan percent” — the orphan confuses the formatter and the output is malformed SQL.

The correct shape

$wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}my_table WHERE column LIKE %s",
    '%' . $text . '%'
);

Two changes:

  • The SQL string has only a clean %s placeholder — no literal % around it.
  • The value being passed in is already wrapped with '%'‘s — PHP concatenation, before prepare() sees it.

prepare() then wraps the whole bound value in quotes and produces valid SQL: WHERE column LIKE '%my search%'.

Add esc_like() for user input

$like = '%' . $wpdb->esc_like( $text ) . '%';

$sql = $wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}my_table WHERE column LIKE %s",
    $like
);

$wpdb->esc_like() escapes MySQL’s LIKE metacharacters — the literal % and _ a user might type. Without it, a search for “50% off” behaves as “starts with 50” (the % becomes a wildcard); with it, the search matches only the literal string.

Two escape stages at play:

  • esc_like() — escapes LIKE-specific metacharacters so user input matches as written.
  • $wpdb->prepare() — escapes SQL-injection risks (quotes, null bytes, backslashes).

Both are needed for user-supplied search strings.

wordpress prepare like sql — %s in query, % wildcards in value, esc_like for user input

Common patterns

// 1. Match anywhere: %text%
$like = '%' . $wpdb->esc_like( $text ) . '%';

// 2. Starts-with: text%
$like = $wpdb->esc_like( $text ) . '%';

// 3. Ends-with: %text
$like = '%' . $wpdb->esc_like( $text );

// 4. Multi-column LIKE with OR
$sql = $wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}my_table
     WHERE column_a LIKE %s OR column_b LIKE %s",
    $like,
    $like
);

The multi-column form passes the same $like value twice — one for each %s. prepare() substitutes positional arguments in order, so you can bind the same variable multiple times without repeating the concatenation.

When to skip $wpdb

WordPress’s built-in query classes already handle LIKE correctly — you just pass the search term and WordPress wraps the wildcards internally:

// Posts
$query = new WP_Query( array( 's' => $text ) );

// Users — see the dedicated guide for search_columns
$users = new WP_User_Query( array(
    'search'         => '*' . $text . '*',
    'search_columns' => array( 'user_login', 'user_email', 'display_name' ),
) );

Drop to raw $wpdb for custom tables (your plugin’s own schema) or for joins WordPress’s classes don’t expose. For anything WordPress provides a class for, the class version is shorter, cached correctly, and less error-prone.

Frequently asked questions

What’s the correct wordpress prepare like pattern?

Use %s as the placeholder in the query and wrap the value with literal % characters: $wpdb->prepare("SELECT * FROM ... WHERE column LIKE %s", '%' . $text . '%'). prepare() handles the escaping of the value (including MySQL LIKE metacharacters if you pass through $wpdb->esc_like()) and returns a ready-to-execute SQL string. Never put the % inside the placeholder itself — that’s what breaks.

Why does LIKE %%s% not work?

prepare() uses printf-style formatting, so %% is a literal percent sign and %s is the string placeholder. The sequence %%s% reads as: literal %, placeholder, orphan literal % — which confuses the formatter and typically produces malformed SQL. The safe pattern always puts the wildcards in the value, not the query string.

Should I still call esc_like()?

Yes, if the value comes from user input. esc_like() escapes MySQL’s LIKE metacharacters (% and _), so a search for "50%" literally matches fifty-percent instead of “starts with 50.” Typical shape: '%' . $wpdb->esc_like( $text ) . '%'. prepare() then handles SQL-level escaping on top.

Can I use %d or %f with LIKE?

No — LIKE is a string operator. %d (integer) and %f (float) are for exact-match comparisons (WHERE id = %d). For wildcard searches always use %s, even when the column you’re searching holds numeric-looking content, because MySQL converts the column to a string before the LIKE comparison runs.

Is there a non-$wpdb way to run LIKE queries?

For posts and users, WordPress’s query classes handle LIKE natively — WP_Query‘s s parameter, WP_User_Query‘s search + search_columns. See How to Search Users by Multiple Fields. Drop to $wpdb only for custom tables (your plugin’s own schema) or joins WordPress doesn’t expose.

Related guides

  • How to Retrieve the Last Inserted Row ID in WordPress — the companion $wpdb pattern for INSERTs.
  • How to Search Users by Multiple Fields in WordPress — built-in class equivalent for user search.
  • How to Order Posts by Meta Value in WordPress — another WP_Query pattern instead of raw SQL.
  • How to Get Posts by Date Range in WordPress — date_query vs raw $wpdb.

References

WordPress developer reference for $wpdb->prepare and $wpdb->esc_like: developer.wordpress.org/reference/classes/wpdb/prepare.

TAGGED:phpSecuritysqlwordpress

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 WordPress order posts by meta value — WP_Query or pre_get_posts with meta_value_num How to Order Posts by Meta Value in WordPress
Next Article WordPress $wpdb->insert_id — read after $wpdb->insert or $wpdb->query INSERT How to Retrieve the Last Inserted Row ID in WordPress
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

You Might Also Like

Check if GD library is installed in PHP (phpinfo and extension_loaded)
Web Development

How to Check if GD Library Is Installed in PHP (3 Easy Methods)

5 Min Read
Fix Laravel CSRF token mismatch in DataTables Ajax
Web Development

How to Fix Laravel CSRF Token Mismatch in DataTables Ajax

4 Min Read
Enforce a minimum phone-number length in WooCommerce checkout
Web Development

How to Set a Minimum Length for Phone Numbers in WooCommerce

6 Min Read
GitHub Actions workflow deploying Laravel to a VPS, zero-downtime symlink swap
Web Development

How to Deploy a Laravel App to a VPS with GitHub Actions (Zero-Downtime, No Forge)

11 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 Console Update Screen
  • Fake Disk Format
  • Fake Low Battery
  • Fake Virus Scan
  • 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?