Reputation: 4110
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
Upvotes: 12
Views: 92166
Reputation: 31
<?php
$user_id = 1;
global $wpdb;
$wpdb_prefix = $wpdb->prefix;
$wpdb_tablename = $wpdb_prefix.'Table_Name';
$result = $wpdb->get_results(sprintf('SELECT `colum1`, `colum2` FROM `%2$s` WHERE `user_id` = %d LIMIT 1', $user_id, $wpdb_tablename));
print_r($result); exit;
?>
Upvotes: 3
Reputation: 81
$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
Upvotes: 7
Reputation: 5183
$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); /
Upvotes: 39