'How to filter elements by meta key from dropdown menu
I'm building a site to show Artists with a list of their releases.
Here's Madonna's Page as an example.
Each page has a list of filter options in a dropdown menu. I'd like to auto-populate those dropdowns with each unique item from a meta-key. Examples are "country of release" and "fortmat". So in the dropdown menu for "format", I'd like it to autopopulate with each type of format she has a release for. Then when you click on one, I'd like to filter the results to only show that format.
Same with year, country, etc. I have the list auto-populating and showing "Releases" but I don't know how to apply a filter to that one.
I can't get the rest to show up. Here's the code for the working dropdown "Releases"
<select>
<option selected="selected">Label</option>
<?php
$format_args = array(
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'wpcf-format',
)
)
);
$format_posts = types_child_posts('release', $format_args);
// sort alphabetically by firstname and lastname
usort($format_posts, 'compare_fullname');
$url = esc_url( home_url() );
?>
<?php foreach ($format_posts as $format_post) {
echo "<option value='Releases'>$format_post</option>";
}
?>
</select>
<select>
<option selected="selected">Picture Disc</option>
<option value="All Releases">All Releases</option>
<option value="Picture Disc Only">Picture Disc Only</option>
<option value="No Picture Discs">No Picture Discs</option>
</select>
<select>
<option selected="selected">Bootleg</option>
<option value="All Releases">All Releases</option>
<option value="Bootleg Only">Bootleg Only</option>
<option value="No Bootlegs">No Bootleg</option>
</select>
Here's the code for one I want to work, but isn't populating.
<select>
<option selected="selected">Year</option>
<?php
$year_args = array(
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'wpcf-year-release',
)
)
);
$year_posts = types_child_posts('release', $year_args);
$url = esc_url( home_url() );
?>
<?php foreach ($year_posts as $child_post) {
echo "<option value='year'>$child_post->wpcf-year-release</option>";
}
?>
</select>
How do I apply a filter to the releases shown so that only releases matching the selected meta-key from the dropdown menu are shown?
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
