select query in wordpress

72,414

Solution 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); /

Solution 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

Solution 3

<?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;
?>
Share:
72,414
Dinesh
Author by

Dinesh

You never finish a program, you just stop working on that.

Updated on July 28, 2022

Comments

  • Dinesh
    Dinesh almost 2 years

    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

  • James Heald
    James Heald almost 8 years
    Is the $from part SQL injection safe?