WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i use a like %text% statement in wordpress:

SELECT *  FROM location  WHERE name LIKE "%on" OR name LIKE "%on%"

i tried anything but it's not run:

$wpdb->get_results($wpdb -> prepare("SELECT *  FROM location  WHERE name LIKE %1s OR name LIKE %2s", '%'.like_escape('on'), '%'.like_escape('on').'%'), ARRAY_A);

please help me

share|improve this question
up vote 4 down vote accepted

$wpdb->prepare() doesn't actually fully support sprintf() placeholders.

From the Codex:

The query parameter for prepare accepts sprintf()-like placeholders. The %s (string), %d (integer) and %f (float) formats are supported.

It's not entirely obvious from that but the argument swapping formats (e.g. %1$d) are not supported (in any case you have the incorrect syntax: it should be %2$s instead of %2s)

global $wpdb;
        "SELECT *  FROM location  
         WHERE name LIKE %s OR name LIKE %s", 
), ARRAY_A);

I'm assuming 'on' might be replaced by a unknown value - otherwise $wpdb->prepare() isn't necessary. On an unrelated note, the second condition in your SQL makes the first obsolete.

share|improve this answer
very clearly, thanks you so much – rocky Aug 15 '13 at 14:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.