WordPress:wpdb准备条件
问题描述:
I\'m trying to use sql->prepare
with many strings concatenation.
In my code below, I try to call a function which will return me an array of get_post
from $wpdb->prepare
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function test($array){ $sqlprep = \"SELECT SQL_CALC_FOUND_ROWS p.ID FROM {$wpdb->prefix}posts p LEFT JOIN {$wpdb->prefix}postmeta m ON m.post_id = p.ID LEFT JOIN {$wpdb->prefix}term_relationships r ON (p.ID = r.object_id) LEFT JOIN {$wpdb->prefix}term_relationships r1 ON (p.ID = r1.object_id) LEFT JOIN {$wpdb->prefix}term_relationships r2 ON (p.ID = r2.object_id) LEFT JOIN {$wpdb->prefix}term_taxonomy tt ON tt.term_taxonomy_id = r.term_taxonomy_id LEFT JOIN {$wpdb->prefix}terms t ON t.term_id = tt.term_id AND t.term_id = r.term_taxonomy_id WHERE 1=1\"; if(isset($array[\'post_type\']) && !empty($array[\'post_type\']){ $sqlprep .= \" AND p.post_type = \'\".$array[\'post_type\'].\"\'\"; } if(isset($array[\'post_type\']) && !empty($array[\'post_type\']){ $sqlprep .= \" AND p.post_type = \'\".$array[\'post_status\'].\"\'\"; } $sql = $wpdb->prepare($sqlprep); $ids = $wpdb->get_col($sql); return array_map(\'get_post\', $ids) } |
and the value is an array which is :
1 2 3 4 5 |
$args=array( \'post_type\' => \'post\', \'post_status\' => \'published\', ); |
When I try to print_r($sql)
to get the query, it always just stop till WHERE 1=1
.
Can string concatenation be used for wpdb prepare
?
网友观点:
Pay attention to the use of the properties in the $wpdb object and the use of the prepare method, furthermore there is an issue with the column name for the post_status:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function test($array){ $sql = \\\"SELECT SQL_CALC_FOUND_ROWS p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID LEFT JOIN {$wpdb->term_relationships} r ON (p.ID = r.object_id) LEFT JOIN {$wpdb->term_relationships} r1 ON (p.ID = r1.object_id) LEFT JOIN {$wpdb->term_relationships} r2 ON (p.ID = r2.object_id) LEFT JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = r.term_taxonomy_id LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id AND t.term_id = r.term_taxonomy_id WHERE 1=1\\\"; if(isset($array[\'post_type\']) && !empty($array[\'post_type\']){ $sql .= $wpdb->prepare(\\\" AND p.post_type = %s\\\", $array[\'post_type\']); } if(isset($array[\'post_type\']) && !empty($array[\'post_type\']){ $sql .= $wpdb->prepare(\\\" AND p.post_status = %s\\\", $array[\'post_status\']); } $ids = $wpdb->get_col($sql); return array_map(\'get_post\', $ids) } |
由于网站搬家,部分链接失效,如无法下载,请联系站长!谢谢支持!
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » WordPress:wpdb准备条件
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » WordPress:wpdb准备条件