admin管理员组

文章数量:1426518

I get no result unless i cut the string title to "Gocomma 10W QI Wireless Fast Charger Car Mount Holder " ...

   $title1="Gocomma 10W QI Wireless Fast Charger Car Mount Holder - 2pcs (it)";  

     $sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_title LIKE '%%%s%%' and post_status='publish'",$title1);

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

any help appreciated!

Edit:

Well i had to save the text from $title1=get_the_title(); to .txt file and i noticed that the – saved as – in the txt file ...then i replaced str_replace("–","-","Holder – 2pcs") and it works! the problem is that in my wordpress databse the title contains - char as it should but then when i use get_the_title(); function of wordpress in my code to retrieve the title i get the - char as – which is eventually – i dont know why get_the_title(); causing this issue! any thoughts?

I get no result unless i cut the string title to "Gocomma 10W QI Wireless Fast Charger Car Mount Holder " ...

   $title1="Gocomma 10W QI Wireless Fast Charger Car Mount Holder - 2pcs (it)";  

     $sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_title LIKE '%%%s%%' and post_status='publish'",$title1);

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

any help appreciated!

Edit:

Well i had to save the text from $title1=get_the_title(); to .txt file and i noticed that the – saved as – in the txt file ...then i replaced str_replace("–","-","Holder – 2pcs") and it works! the problem is that in my wordpress databse the title contains - char as it should but then when i use get_the_title(); function of wordpress in my code to retrieve the title i get the - char as – which is eventually – i dont know why get_the_title(); causing this issue! any thoughts?

Share Improve this question edited Jun 23, 2019 at 13:30 stefanosn asked Jun 23, 2019 at 9:54 stefanosnstefanosn 1339 bronze badges 1
  • 1 Welcome to WordPress Stack Exchange! Just stating facts and asking for "any help" unfortunately makes this a too broad question. – norman.lol Commented Jun 23, 2019 at 10:37
Add a comment  | 

1 Answer 1

Reset to default 1

First of all, you escape that query incorrectly... You should use esc_like on the like part and prepare should be used a little bit different:

$title1 = 'Gocomma 10W QI Wireless Fast Charger Car Mount Holder - 2pcs (it)';
$sql = $wpdb->prepare(
           "SELECT * FROM {$wpdb->posts} WHERE post_title LIKE %s AND post_status = 'publish'",
           '%' . $wpdb->esc_like($title1) . '%'
       );
$post_if = $wpdb->get_results( $sql );

But there is one more problem with this approach. There is a chance that the - character gets converted in some other character (like - dash) and so on.

本文标签: sqlquery using wpdb in wordpress gets me no result