Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select query in wordpress

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

like image 809
Dinesh Avatar asked Oct 21 '13 11:10

Dinesh


People also ask

How do I select a query in WordPress?

I create a new WordPress query using the WP_Query class using $wpdb->get_results. also WordPress is written using PHP and MySQL. $post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'yiik-new-url123' AND meta_value = '".

How do I use a database query in WordPress?

Below is an example of querying the database for posts within a category using WP_Query class. $query = new WP_Query( 'cat=12' ); The result will contain all posts within that category which can then be displayed using a template. Developers can also query WordPress database directly by calling in the $wpdb class.


1 Answers

$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); /
like image 197
Prince Singh Avatar answered Oct 05 '22 23:10

Prince Singh