Populate select list with meta values from all posts of a Custom Post Type

I've got a CPT of Inventory and I'm trying to show a dropdown of all Mfrs. I've got the form and pre_get_posts function so that when a user selects a Mfr, the page refreshes and shows only the inventory with that Mfr.

THE PROBLEM: Once you select a Mfr. and the page refreshes, only that mfr shows as an option! No matter what I try (I won't post them all here because I have tried like 10 different loops) to build the select options, I can't get it to show ALL Mfrs. all the time.

I've read through several dozen articles (literally) and cannot seem to find a solution that works.

Here is what I have right now:

FUNCTIONS.PHP

function my_pre_get_posts( $query ) {
    // do not modify queries in the admin
    if( is_admin() ) {
        return $query;      
    }

    // only modify queries for 'inventory' post type
    if( isset($query-query_vars['post_type'])  $query-query_vars['post_type'] == 'inventory' ) {
        
        // allow the url to alter the query
        if( isset($_GET['item_oem']) ) {
            $query-set('meta_key', 'item_oem');
            $query-set('meta_value', $_GET['item_oem']);
            $query-set( 'meta_compare', '=' );
        } 
    }
    // return
    return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');

LOOP:

        ?php // using get right now to see the query string and confirm functionality ? 
        form method=get action= onchange=submit();

            ?php 
                $posts = get_posts( array(
                'post_type' = 'inventory',
                'posts_per_page' = -1,
                'orderby' = 'date',
                'order' = 'DESC',
            ));

            if ( $posts ): ?

            select name=item_oem id=select-oem
                ?php foreach( $posts as $post ): ?
                    option value=?php the_field('item_oem'); ? ?php the_field('item_oem'); ? /option
                ?php endforeach; ?
            /select
        ?php endif; ?
        ?php wp_reset_postdata(); ?

        /form

So, this DOES give me a full list of Mfrs. But, once you select one and it reloads the page to only show inventory with that Mfr., then the selected Mfr is the only option in the select.

How do I override that and always show ALL meta values for the 'item_oem' meta key??

Thanks so much!

Topic advanced-custom-fields select loop get-posts wp-query Wordpress

Category Web


Here is the solution I came up with, that works properly.

<form method="get" action="" onchange="submit();">
        <div class="input-group">
            <?php
            global $wpdb;
            $meta_key = 'item_oem';
            $data = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key) , ARRAY_N  ); ?>
            <select class="custom-select" id="oems" aria-label="oems" name="item_oem" >
                <option selected>Search By OEM</option>
                <?php foreach( $data as $value ): ?>
                    <option value="<?php echo $value[0]; ?>"><?php echo $value[0]; ?> </option>
                <?php endforeach; ?>
          </select>
          <div class="input-group-append">
            <a href="/inventory" class="btn btn-secondary" type="button">Clear Filter</a>
          </div>
        </div>
    </form>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.