Multiple meta_value for same page
-
Hello,
We’ve got a collection of pages which can have multiple custom dates and locations, saved as custom fields; these are our site’s course listings, hence why a page can have multiple meta_values for the same meta_key.
We would like to display a list of the next five courses to run and managed to do this with a select query. What we’re not managing, however, is to retrieve the correct date for each listing. The list might be something like this:
Course 1
Course 2
Course 1
Course 3
Course 2Using
<?php echo get_post_meta($post->ID, 'startdatum', true); ?>
only returns the first (earliest) meta_value, hence both listings for “Course 1” get the same date.Is there a way to get the “correct” date for a course? The query manages to sort the pages by a specific meta_value, and I’d then like to echo this specific value that is used for sorting, not the first meta_value. Perhaps one can iterate somehow, so that if the query outputs the page a second time, then it’ll use the second meta_value? Of course, then there is the problem of old dates still attached to the page..
I’m not sure if there is a way, with the help of a “normal” select query. I should mention that we’re using the Flutter plugin to add dates (group of start date, end date and location), maybe the only way is to use some Flutter-specific code.
I’ll post back if I come up with a solution.
*****
<?php $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'startdatum' AND wpostmeta.meta_value > NOW() AND wposts.post_status = 'publish' AND wposts.post_type = 'page' ORDER BY wpostmeta.meta_value ASC LIMIT 5 "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?> <?php if ($pageposts): ?> <?php foreach ($pageposts as $post): ?> <?php setup_postdata($post); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br /> <?php echo get_post_meta($post->ID, 'startdatum', true); ?> <?php endforeach; ?> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php endif; ?>
- The topic ‘Multiple meta_value for same page’ is closed to new replies.