• Hi

    I have the following query code that I need to modify to also search by a specific meta_key and meta_value pair in a specific post-type. I have tried changing the code but I am getting empty results.

    This is the original code:

    $letters = $wpdb->get_col(
    "SELECT DISTINCT LEFT(post_title,1) AS first_letter FROM $wpdb->posts
    WHERE post_type = '$post_type' AND post_status = 'publish'
    ORDER BY first_letter ASC"
    );

    I tried to change it to:

    $post_type = 'directory-listing';
    $meta_key = 'services';
    $meta_value = 'Book Storage';
    
    $letters = $wpdb->get_col(
    "SELECT DISTINCT LEFT($wpdb->posts.ID_title,1) AS first_letter FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND
      $wpdb->posts.post_status = 'publish' AND
      $wpdb->posts.post_type = '$post_type' AND
      $wpdb->postmeta.meta_key = '$meta_key' AND
      meta_value = '$meta_value'
    ORDER BY first_letter ASC"
    );

    As I said I get an empty result.

    Can anyone spot my issue?

    Thanks in advance for your help!

  • The topic ‘modifying SELECT to query by meta-key and meta-value’ is closed to new replies.