Ok, if i understand you clearly, then this is the solution
// first get the post ids that have the breakfast food type
$post_ids = $wpdb->get_col( "SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'type' AND meta_value="Breakfast"" );
// now get the food names
$stocktypes = $wpdb->get_col( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'food' AND post_id IN (". implode(',', array_map('absint', $post_ids) ) .")" );
if ($stocktypes) {
foreach ($stocktypes as $stocktype) {
echo "<option value=\"" . $stocktype . "\">" . $stocktype . "</option>";
}
}
Related Posts:
- Get multiple custom field values in a $wpdb query [duplicate]
- wpdb function insert doesn’t work
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- SELECT max(meta_value) FROM wp_postmeta WHERE meta_key=’price’… stops working when value is over 999
- So much data in postmeta
- What is the advantage of the wp_options design pattern?
- importing data from non-wordpress mysql db
- Query meta field using between
- Move value of one custom field to another
- How to filter a dd/mm/yyyy date from a custom field in a query
- Difference between ‘LIKE’ and ‘IN’ in meta queries
- little help with a mySQL query to wp database
- Importing users to Buddypress with custom fields
- MySQL query to set wp_postmeta using term_taxonomy_id value
- MySQL Query that looks for post with Custom Field, then changes Category
- Unique Post Meta Values
- Add custom field to all posts in specific post_type
- UPDATE on SELECT results. A SQL query to swap Author with Meta Box value
- Custom Query based on custom field of a single post
- Build index page from custom fields
- Importing WordPress Posts With Custom Meta Data As CSV Files Through PHPMyAdmin
- Inserting data into MagicFields using mysql queries
- Copy price categories to custom field
- If I update WordPress my custom data will be deleted from the wp_users table?
- Get emails from users by custom field SQL
- Update post meta data (custom field) by post link
- How to improve my non-unique metadata MySQL entries?
- What is the correct way to search 3 custom fields only in WordPress?
- Delete a custom field in mysql for all posts with specific category id
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- use mysql to replace one custom field value with another custom field value
- Update custom table on metabox POST
- ACF plugin and field update
- Query postmeta values, and return multiple post_titles for common meta value
- Query Custom Fields in Searchform
- Copy url from post_content to custom field
- Convert many posts from having a specific meta_key to use a post_format
- Custom Search, MySql Query Gone Wrong?
- Site uses wpdb to fetch meta_keys but just displays first meta_key from a page (the post uses the same meta_key “filmmaker” more than once)
- Can running a WP-Cron to update_post_meta cause performance issues?
- Using wpdb to connect to a separate database
- Using wordpress post data as input for google visualisations
- Order posts by custom field and if custom field is empty return remaining posts
- wpdb Custom Meta Data with 2 conditions
- Can serialized arrays in DB be matched against serialized arrays with meta_query?
- Create a select drop down of available menus
- Adding a prefix to a post title
- copy attachments to another post type and change attachment url
- Cannot edit post meta fields with rest API
- Only display custom field if it there is a value inside
- Custom field values repeating
- Sorting posts by multiple values, combined
- Send Multiple Custom Field Values Through the URL
- Separate table or usermeta
- Woocommerce with metabox plugin
- using multiple meta_key and meta_value in query_posts
- Json api does not return all metadata
- Use custom field as a variable
- WordPress database error – Error in SQL syntax – I can’t identify any error?
- ACF: How to get the full field name (meta_key) by a field key?
- global function to apply filter to custom field
- Print custom field in Query Loop block
- Is there a simpler version of WP Media?
- Order RSS content by an advanced custom field value
- modify wordpress caption shortcode
- Alter main archive, to show posts with meta as last
- Add microdata from schema.org [closed]
- Displaying multiple URLs as custom field values
- How can I save custom meta to one global value?
- get posts where a custom field contains a text
- Custom WordPress Table wpdb
- How to use media upload on metabox post page without breaking TinyMCE?
- Branch made by several custom values
- User customising position of WordPress Featured Image
- Insert image into sub-field with update_post_meta
- How to use conditional statement with custom field
- Custom field php
- The best way to collision check in WP
- Get meta field value of all catgories
- Filter post style attribute
- Update some (not all) post titles with custom field values before running the Loop
- Arrays in custom fields or is there a better way?
- Setting user permissions per post
- Adding Facebook’s image and meta description retrieval capabilities to a WordPress post
- Weird custom fields data lost
- WooCommerce 3 get custom variation on process order [closed]
- query_posts -> get page_id from custom field
- Output Custom Field value to shortcode
- display custom media folders to wordpress media popup
- Should I store arrays in custom fields or create custom tables?
- Custom Meta Boxes and Fields for WordPress: Change directory upload based on user-edit page
- MySQL database migration to WordPress
- Get custom field from other post in carousel
- Form fields based on custom fields
- show custom field content in jquery tabs
- Create citation and url in post using 3 custom fields with conditions for each field… So close!
- woocommerce how in checkout to add my custom field check box, and when i check it hide the shipping adress and add tax 19% [closed]
- Show only first image of multiple image field
- Custom Fields after update to WordPress 6
- Archive for custom fields?