Yes, do an INNER JOIN
, something like this should work:
$values = $wpdb->get_col($wpdb->prepare("
SELECT meta_value FROM $wpdb->postmeta
INNER JOIN $wpdb->posts
{$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
WHERE meta_key= %s
AND {$wpdb->posts}.post_status="publish""
,$metakey
));
I changed $wpdb->get_results()
to $wpdb->get_col()
since it looks like you are just selecting a single column of data as an array. I also added $wpdb->prepare()
since that is generally best practice.
Related Posts:
- Triple meta_key on custom SELECT query
- How to get all term meta for a taxonomy – getting term_meta for taxonomy
- Short of raw SQL, can I query for multiple attachment metadata that have a given array key?
- Get updated post meta on save_post action?
- MySQL Query To Select Post By Postmeta
- Set Condition echo function get_post_meta
- Get a row from a separate table by matching a posts meta_key to a tables ID column
- Compare string with post id in wpdb and do stuff when match is found
- How to test the outcome of a wpdb query?
- wpdb->get_var – count author posts, meta value
- How to query and update one colum in postmeta table?
- Display current ranking of post as a number in post title
- get specific values from WordPress meta_value
- Get data from custom table and update relative post_meta based on meta_key
- Get table parameter and save in meta value
- Exclude category from DB query
- Fetching array of postmeta with $wpdb and in_array conditional
- Which query method to use? (edit- wpdb syntax problems)
- Cache metadata for set of posts
- How to get all the related ids without array?
- How do I retrieve the slug of the current page?
- How to extract data from a post meta serialized array?
- How to save an array with one metakey in postmeta?
- WordPress is stripping escape backslashes from JSON strings in post_meta
- Lack of composite indexes for meta tables
- So much data in postmeta
- Content hooks vs User hooks
- Why is get_post_meta returning an array when I specify it as single?
- WPDB Multiple site’s posts and get featured images
- Save both current and new version of post meta
- get_post_meta – get a single value
- get_post_meta / update_post_meta array
- update_post_meta not adding anything.(Nor add_post_meta)
- Are multiple values from get_post_meta guaranteed to be ordered?
- Identifying Importer Posts
- How metadata API works?
- How to sanitize post meta field value?
- Post IDs missing on delete_postmeta action hook
- How trigger to save post when updating post meta
- Create a Metabox that behaves Like a Taxonomy Box
- Views count with time limit per IP
- order by multiple meta_keys?
- Multiple postmeta with same name for one post in wp_postmeta table
- Echo page title from page with custom meta datetime picker
- echo image caption
- get_*_meta doesn’t always return an array
- Generate multiple goo.gl shortlinks for qtranslate bilingual blog
- MySQL Rank & $wpdb
- How can I convert postmeta from unserialized to serialized?
- add_post_meta adds meta to one more different post
- Custom query, multiple custom keys
- Trying to get property of non-object error using get_post_meta()
- How to get last user with wpdb?
- Adding new value to existing meta key
- how to get a list of meta data fields assocaited with a custom post type
- How to insert multiple postmeta values with one query?
- Gutenberg: How to display meta field data in the block frontend (save function)
- String taken from database table post_meta is showing HTML tags on Frontend
- Have lots of meta for posts, is it better to get at all at once or each individually
- Delete posts based on post meta data
- how to get serialized post meta
- How do I edit the posted “by” and “on” in the post meta
- How to delete duplicate records in wp_postmeta database table?
- update_post_meta() adding bits in database [closed]
- Get posts ordered by meta data
- Get results from wordpress data custom table
- Displaying values from related field – if empty shows current post type’s link and title
- get_post_meta inside echo [closed]
- WordPress postmeta: Store data as an array or single item?
- Display Post Meta in Header
- Array of user ids to list of user names
- Conditional For get_the_author_meta
- Custom fields or something else
- Count Post and Page Views based on meta_value Using Shortcode in Dashboard Widget
- I want to Add Feedburner Right After Meta Details
- if get_post_meta function returns empty – Do Not Display HTML
- SELECT rows between two datetimes when the range is dynamic [closed]
- Custom column on CPT not showing correct value when meta data not set
- How to access post meta on the first time a post is published
- I Can’t get the post_tag name from term_id using wp_set_object_terms
- How to update/add child posts meta whenever the parent post meta is updated?
- How to query post with meta?
- why is my postmeta table is so heavy
- update_post_meta() throws Uncaught error: Cannot create duplicate attribute
- Using a comma instead of a pipe to separate metadata
- show user based on user_meta
- Meta keys won’t add using wp_insert_post
- Re-pointing images to cloud storage
- undesrtanding get_post_meta function
- How Can I extract Image urls from post meta
- Twenty Twelve entry meta data – change and move
- How can I prevent the post_modified column in wp_posts from being updated?
- Sorting meta box values by start- and enddate and all dates in between
- Use an id on a button to update_post_meta in post
- get_post_meta image width for lazy load
- Custom Fields help
- WP-API : post_meta not updated… but have another entry
- Parse error thrown by get_post_meta [closed]
- Remove action of an external plugin after checking if custom post meta is set
- get_post_meta() returns nothing in save_post, publish_post, wp_after_insert_post