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!