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?
- 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
1 Answer
Reset to default 1First 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
版权声明:本文标题:sql - query using wpdb in wordpress gets me no result 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745376394a2655955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论