some of my posts have the post meta of “project_id”. How can I retrive all the posts which have the meta key of “project_id” ?
I tried:
$meta_key = 'project_id';
return $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
It returns nothing. Please help! Thanks!
As simple as this:
$the_posts_you_want = get_posts( array( 'meta_key' => 'project_id' ) );
Your query return nothing since there is no field
ID
in the postmeta table so it really should bepost_id
: