我有一个位代码来获取所有postmeta(WordPress)与$ wpdb方法,但我不知道为什么代码不起作用。
global $wpdb;
$meta_key = "'_bbp_vs_name_1', '_bbp_vs_name_2'";
$tags_query = "Po";
$query = $wpdb->esc_like( $tags_query );
$value = '%' . $query . '%';
$ajax_teams = $wpdb->get_results( $wpdb->prepare( "
SELECT *
FROM $wpdb->postmeta
WHERE meta_key IN ( $meta_key )
AND meta_value LIKE %s
",
$meta_key,
$value
) );
print_r( $ajax_teams );
请帮忙。
工作解决方案:
$ajax_teams = $wpdb->get_results( $wpdb->prepare( "
SELECT *
FROM $wpdb->postmeta
WHERE meta_key IN ( $meta_key )
AND meta_value LIKE %s
",
$value
) );
答案 0 :(得分:0)
您需要在查询中绑定%s。但是你已经给出了可变的方向
$ajax_teams = $wpdb->get_results( $wpdb->prepare( "
SELECT *
FROM $wpdb->postmeta
WHERE meta_key IN ( %s )
AND meta_value LIKE %s
",
$meta_key,
$value
) );
同时将值更改为:
$value = "'%".$query."%'";