<?xml version="1.0" encoding="UTF-8"?><!-- generator="bbPress" -->

<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>

<channel>
<title>WordPress &#8250; Support Topic: Multiple meta_value for same page</title>
<link>http://wordpress.org/support/</link>
<description>WordPress &#8250; Support Topic: Multiple meta_value for same page</description>
<language>en</language>
<pubDate>Thu, 26 Nov 2009 14:51:55 +0000</pubDate>

<item>
<title>karl19 on "Multiple meta_value for same page"</title>
<link>http://wordpress.org/support/topic/281080#post-1157883</link>
<pubDate>Fri, 31 Jul 2009 11:07:11 +0000</pubDate>
<dc:creator>karl19</dc:creator>
<guid isPermaLink="false">1157883@http://wordpress.org/support/</guid>
<description>&#60;p&#62;In the end we had someone help us figure this out. It might be that the solution is only suitable if you're using the Flutter plugin, I'm not entirely sure, perhaps any custom field is fine.&#60;/p&#62;
&#60;p&#62;Below is the code, perhaps it can help someone.&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;&#38;lt;?php
//Get today date
$today = strtotime(&#38;quot;now&#38;quot;);

//Revised query string
$querystr = &#38;quot;
	SELECT wposts.*
	FROM $wpdb-&#38;gt;posts wposts, $wpdb-&#38;gt;postmeta wpostmeta
	WHERE wposts.ID = wpostmeta.post_id
	AND wpostmeta.meta_key = &#38;#39;startdatum&#38;#39;
	AND wpostmeta.meta_value &#38;gt; &#38;#39;&#38;quot; . date(&#38;quot;Y-m-d&#38;quot;, $today) . &#38;quot;&#38;#39;
	AND wposts.post_status = &#38;#39;publish&#38;#39;
	AND wposts.post_type = &#38;#39;page&#38;#39;
	ORDER BY wpostmeta.meta_value ASC
	LIMIT 0, 3
&#38;quot;;

$dateIndexes = array();
$pageposts = $wpdb-&#38;gt;get_results($querystr);
?&#38;gt;

&#38;lt;?php if ($pageposts): ?&#38;gt;
&#38;lt;ul&#38;gt;
&#38;lt;?php foreach ($pageposts as $post): ?&#38;gt;
&#38;lt;?php setup_postdata($post);?&#38;gt;

	&#38;lt;li&#38;gt;&#38;lt;a href=&#38;quot;&#38;lt;?php the_permalink() ?&#38;gt;&#38;quot;&#38;gt;&#38;lt;b&#38;gt;&#38;lt;?php the_title(); ?&#38;gt;&#38;lt;/b&#38;gt;&#38;lt;br /&#38;gt;
	&#38;lt;?php
		//Get start date value
		$startdate_values = get_post_custom_values(&#38;#39;startdatum&#38;#39;);
		$startdate_values_copy = $startdate_values;
		$start = 0;

		if (strlen($dateIndexes[$post-&#38;gt;ID]) &#38;gt; 0 &#38;amp;&#38;amp; $dateIndexes[$post-&#38;gt;ID]&#38;gt;=0)
		{
			$start = $dateIndexes[$post-&#38;gt;ID]+1;
		}

		$location_values = get_post_custom_values(&#38;#39;ort&#38;#39;);

		sort($startdate_values);
		for ($i=$start; $i&#38;lt; count($startdate_values); $i++)
		{
			//Convert to date as it is stored as string in database
			$nextdate = strtotime($startdate_values[$i]);

			//Check if the date equals or greater than today
			if ($nextdate&#38;gt;=$today)
			{
				//Print out
				echo strftime(&#38;#39;%e %b, %Y&#38;#39;, $nextdate);

				$index = -1;
				//Find the right index before sortig
				for ($j=$start; $j&#38;lt; count($startdate_values_copy); $j++)
				{
					if ($startdate_values_copy[$j] == $startdate_values[$i])
					{
						$index = $j;
						break;
					}
				}

				//Put into an array
				$dateIndexes[$post-&#38;gt;ID] = $i;

				//If found, break the loop
				break;
			}
		}
	?&#38;gt;&#38;lt;/a&#38;gt;&#38;lt;/li&#38;gt;

&#38;lt;?php endforeach; ?&#38;gt;
&#38;lt;/ul&#38;gt;
&#38;lt;?php else : ?&#38;gt;
&#38;lt;p&#38;gt;No upcoming events&#38;lt;/p&#38;gt;
&#38;lt;?php endif; ?&#38;gt;&#60;/code&#62;&#60;/pre&#62;</description>
</item>
<item>
<title>karl19 on "Multiple meta_value for same page"</title>
<link>http://wordpress.org/support/topic/281080#post-1106893</link>
<pubDate>Wed, 17 Jun 2009 09:19:08 +0000</pubDate>
<dc:creator>karl19</dc:creator>
<guid isPermaLink="false">1106893@http://wordpress.org/support/</guid>
<description>&#60;p&#62;Hello,&#60;/p&#62;
&#60;p&#62;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.&#60;/p&#62;
&#60;p&#62;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:&#60;/p&#62;
&#60;p&#62;Course 1&#60;br /&#62;
Course 2&#60;br /&#62;
Course 1&#60;br /&#62;
Course 3&#60;br /&#62;
Course 2&#60;/p&#62;
&#60;p&#62;Using &#60;code&#62;&#38;lt;?php echo get_post_meta($post-&#38;gt;ID, &#38;#39;startdatum&#38;#39;, true); ?&#38;gt;&#60;/code&#62; only returns the first (earliest) meta_value, hence both listings for &#34;Course 1&#34; get the same date.&#60;/p&#62;
&#60;p&#62;Is there a way to get the &#34;correct&#34; 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..&#60;/p&#62;
&#60;p&#62;I'm not sure if there is a way, with the help of a &#34;normal&#34; 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.&#60;/p&#62;
&#60;p&#62;I'll post back if I come up with a solution.&#60;/p&#62;
&#60;p&#62;*****&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;&#38;lt;?php

	$querystr = &#38;quot;
		SELECT wposts.*
		FROM $wpdb-&#38;gt;posts wposts, $wpdb-&#38;gt;postmeta wpostmeta
		WHERE wposts.ID = wpostmeta.post_id
		AND wpostmeta.meta_key = &#38;#39;startdatum&#38;#39;
		AND wpostmeta.meta_value &#38;gt; NOW()
		AND wposts.post_status = &#38;#39;publish&#38;#39;
		AND wposts.post_type = &#38;#39;page&#38;#39;
		ORDER BY wpostmeta.meta_value ASC
		LIMIT 5
	&#38;quot;;

 $pageposts = $wpdb-&#38;gt;get_results($querystr, OBJECT);

 ?&#38;gt;

 &#38;lt;?php if ($pageposts): ?&#38;gt;
 &#38;lt;?php foreach ($pageposts as $post): ?&#38;gt;
 &#38;lt;?php setup_postdata($post); ?&#38;gt;

 &#38;lt;a href=&#38;quot;&#38;lt;?php the_permalink() ?&#38;gt;&#38;quot; rel=&#38;quot;bookmark&#38;quot; title=&#38;quot;Permanent Link to &#38;lt;?php the_title(); ?&#38;gt;&#38;quot;&#38;gt;&#38;lt;?php the_title(); ?&#38;gt;&#38;lt;/a&#38;gt;&#38;lt;br /&#38;gt;
 &#38;lt;?php echo get_post_meta($post-&#38;gt;ID, &#38;#39;startdatum&#38;#39;, true); ?&#38;gt;

 &#38;lt;?php endforeach; ?&#38;gt;
 &#38;lt;?php else : ?&#38;gt;
    &#38;lt;h2 class=&#38;quot;center&#38;quot;&#38;gt;Not Found&#38;lt;/h2&#38;gt;
    &#38;lt;p class=&#38;quot;center&#38;quot;&#38;gt;Sorry, but you are looking for something that isn&#38;#39;t here.&#38;lt;/p&#38;gt;
 &#38;lt;?php endif; ?&#38;gt;&#60;/code&#62;&#60;/pre&#62;</description>
</item>

</channel>
</rss>
