-->

select query in wordpress

2020-08-26 11:17发布

问题:

I am trying to do this by 2 hours. I have custom fields in database and I want to get post_id by the meta keys or meta values. I am doing like this

$post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id); // giving only 140

this is working fine, but this is giving only one post_id and I want all possible post_id matched by meta_value. for example : I have three post 140,141,142, in database. But by this query I am only getting 140. Any Idea how to get all possible post_id by this query or any other way by comparing meta_fields...

Thanks

回答1:

$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id); /


回答2:

$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id);

for more information ... http://codex.wordpress.org/Class_Reference/wpdb