There’s no need for custom SQL here, use the power of meta queries:
$posts = get_posts(
array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'Country',
'value' => 'UK',
),
),
)
);
foreach ( $posts as $post )
$cities[] = get_post_meta( $post->ID, 'city', true );
foreach ( array_unique( $cities ) as $city )
echo $city . '<br />';
Related Posts:
- Multipart/formatted MySQL query problem
- wpdb COALESCE won’t work
- Get posts from category with custom query
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- Updating with $qpdb->query() always returns 0 rows affected
- How to return count of custom post type posts with a specific custom field value via $wpdb?
- Help with Related Posts Function
- Fastest and most efficient SQL query to check if UID exists
- Mysql query and odd results
- How can I create a meta_query with an array as meta_field?
- Return only Count from a wp_query request?
- Differences between wpdb->get_results() and wpdb->query()
- How can I query all users who registered today?
- simple sql query on wp_postmeta very slow
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- How many WordPress SQL Queries per page?
- Search multiple meta keys at once
- Custom $wpdb Query for Custom Post Type by Category
- Can’t pass table to $wpdb->prepare
- WordPress creating excessive joins on meta_query with search
- How to display liked posts of current user in wordpress?
- Are database queries created using WordPress filters protected from SQL injection?
- Grouping related postmeta data via SQL query
- Mysql / WordPress killing my server with 80k users [closed]
- Slow meta_query with about 4 milion record on wp_postmeta
- wpdb::prepare() isn’t working
- How can I apply a meta query for a single custom post type in the main query?
- mySQL query. ORDER BY meta_key
- $wpdb doesn’t appear to work on page inside of a plugin
- Strange string in console from wpdb query
- How to check current user in meta value array in WP_Query meta_query
- Update slug (URL) of pending posts via phpMyAdmin
- Query metas (and not : query posts by metas)
- What’s wrong with my $wpdb prepare?
- $wpdb query a post type within a specific taxonomy term while ordering posts by custom meta value?
- MySQL variable in query
- How to execute a SQL-query which contains multiple queries using $wpdb->query?
- Alter a specific query on WordPress
- Help with MySQL to $WPDB query
- SQL query to select posts from multiple categories
- How do I see the mysql query generated by get_posts( $args )?
- Alter query with posts_clauses to retrieve NULL values last
- How to orderby multiple meta fields with another meta query
- wpdb->term_taxonomy count posts from both published and private posts?
- wpdb print all post meta
- Plugin will sort users by usermeta
- Using $wpdb (WPDB class) ‘replace’ with multiple WHERE criteria problem
- MySql Query very slow
- Searching With Apostrophe
- wpdb query problem to access previous 3 days posts
- WordPress Query wp-terms SLOW
- How to write: $wpdb->update having WHERE NOT value pair in the array
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Query returning same results even though the ID changes
- Custom WordPress Table wpdb
- Custom SQL Query passing array to WHERE p.id
- Placing a widget with $wpdb query
- Custom Query returns array, but I can’t see it’s elements
- how to insert missing tags into the posts through mySQL?
- Query Column of Specific ID from Database Table
- Optimize slow SQL query for multiple meta values
- prepare function sql safe method
- get_post_meta with WP_query
- How do I query for posts by custom meta and those that have been stickied?
- wpdb update multiple row?
- Formatting a date/time returned from a custom $wpdb query
- How to “orderby” the first array in a meta_query that uses multiply keys?
- Improve performance of slow query
- How do I get specific readable results from this query and array results
- meta_query not working
- I want to get on those users their meta value are like “AGENT” .. but this query is not working
- Woocommerce Get Orders By Meta Value
- Delete oldest wordpress post (SQL query)
- WordPress query: merge meta key (number) values and sort
- Displaying data from custom table
- Get 2 meta values from meta key column
- Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
- OR condition not working
- implementing my wp_query using $wpdb class
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- Performance of wp_get_attachment_image_srcset() and wp_get_attachment_image_url()
- How to make MySQL search queries with quotes
- Use $wpdb->get_results with filter based on array
- Query Posts that have or don’t have a meta_value and order by the same ASC
- Display result from custom post meta query
- Wpdb empty result only if language is Russian
- What is the best way to query posts based on live data?
- How to fix this insert? Don’t work for me!
- Problem on wpdb queries, wp_insert_post, wp_insert_user etc. The query runs twice
- Update vs Insert logic but the last key is always inserted?
- Custom MySQL Query for Post and Post Meta
- $wpdb: how to use a query as a base for another query?
- Wpdb generates too many queries
- Custom Query for searching through custom fields
- list or get meta_key where meta_value is ‘something’
- Access Tables with number prefix
- How to reference same column name but different table in custom db query
- Retrieving custom field array value through db query