Getting All Unique Post Meta Values of a Meta Key

I need to retrieve a unique set of meta values of a specific meta key. I found a function from Paul Chimoy which does nearly what I am looking for. I just need 1). a version with a secondary qualification, and 2). a version with a secondary and tertiary qualification in order for it to output what I need.

The function outputs all the meta values for meta_key ‘trees’ and ‘states’ but I need to be able to output, 1). all unique meta values for meta_key ‘trees’ where meta_key ‘states’ equals ‘california’ and 2). all unique values for meta_key ‘trees’ where meta_key ‘states’ equals ‘california’ and meta_key ‘countries’ equals ‘usa’.

Read More
Trees
meta_key = trees
meta_value = pine, oak, sequoia

States
meta_key = states
meta_value = california, washington, florida

Countries
meta_key = countries
meta_value = usa, uk, ireland

Without success, I adjusted the function to include:

AND (pm.meta_key = 'states' AND pm.meta_value = 'california')

Here is the original function without my additional qualification:

 function get_unique_post_meta_values( $key = 'trees', $type = 'post', $status = 'publish' ) {
    global $wpdb;
    if( empty( $key ) )
        return;
    $res = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
    return $res;
}

My limited knowledge is not allowing me to properly adjust the function to my needs. I appreciate any help that can be provided to achieve my goals.

Thanks in advance.

Related posts

1 comment

  1. I know this is old, and you probably are not looking anymore, but if someone else needs this, here is what needs to be done.

    Right now your select statement is:

    SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm
    LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
    WHERE pm.meta_key = '%s'
    AND p.post_status = '%s'
    AND p.post_type = '%s'
    

    That will select the meta value based on one key.

    Instead you want to get the unique meta values of another key based on one key.

    To do this you need to select the post, then select other distinct meta values based on your required key. This means you need to nest your SQL, and grab your post ID.

    SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm WHERE pm.post_id IN 
    (SELECT pm.post_id FROM {$wpdb->postmeta} pm
    LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
    WHERE pm.meta_key = '%s'
    AND pm.meta_value = '%s'
    AND p.post_status = '%s'
    AND p.post_type = '%s') 
    AND pm.meta_key = '%s'
    

    Your wrapping statement should look something like:

    $res = $wpdb->get_col( $wpdb->prepare($sql, $searchkey, $searchvalue, $poststatus, $posttype, $findkey ) );
    

    Assuming you feed in the SQL above, trees, pine, post, publish, states – you should retrieve a list of states where pine trees grow.

    Hope this helps anyone who stumbles upon this!

Comments are closed.