I have a winery website with the custom post type "wine", each wine has various custom meta fields like "vintage", "pH", "Varietal-Composition" etc...
Now, i would like to list all wines where vintage=2005.
Any pointers?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityCheck this : Get posts by meta data OR title
The first piece of code is working and allow to make a query based on meta data
You could just get rid of useless stuff (or adapt if you want to use several keys) :
$args = array(
'post_type' => 'wine'
,'meta_query' => array(
'key' => 'vintage'
,'value' => '2005'
,'compare' => '='
)
);
$query = query_posts( $args );
Querying the posts is a fine way to approach this problem. An alternate way of doing it is by setting up a custom taxonomy for fields with limited values. Vintage is a great choice for this, because you're only going to have so many years. The advantage of doing this with certain fields, is that you get list views, RSS feeds, permalinks, and the like "out of the box". Again, this won't work for every field, but it's likely that it's the best choice for the fields where you'll want to list by a specific value.