wpdb Custom Meta Data with 2 conditions

I found my error in the above query. There still may be a better way to do this, thank you kaiser, and I will update once I’ve done more study. What I forgot to do was GROUP_CONCAT another meta_value so i could pull out different bits. It still seems longer than it needs to be, but it gets the job done fast. Here is the code, just wrap this in a form and select and you can have a drop down of unique meta_values with a few filters.

    $metakey       = $prefix . 'pub_author';
    $authors_query = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT meta_value, post_id, GROUP_CONCAT(meta_value)
                             FROM pyd_postmeta
                             WHERE meta_key = %s
                             GROUP BY meta_value
                             HAVING post_id = ANY (
                             SELECT post_id
                             FROM pyd_postmeta
                             WHERE meta_value="Newsletter"
                             )
            ", $metakey
        )
    );

    if ( $authors_query ) {
        foreach ( $authors_query as $author ) {
            ?>
        <option value="<?php echo $author ?>" <?php selected( $pydnet_show_author, $author ) ?> ><?php echo $author ?></option>';
        <?php
        }
    }

Leave a Comment