• Resolved KS

    (@karl19)


    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 2

    Using <?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; ?>
Viewing 1 replies (of 1 total)
  • Thread Starter KS

    (@karl19)

    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.

    Below is the code, perhaps it can help someone.

    <?php
    //Get today date
    $today = strtotime("now");
    
    //Revised query string
    $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 > '" . date("Y-m-d", $today) . "'
    	AND wposts.post_status = 'publish'
    	AND wposts.post_type = 'page'
    	ORDER BY wpostmeta.meta_value ASC
    	LIMIT 0, 3
    ";
    
    $dateIndexes = array();
    $pageposts = $wpdb->get_results($querystr);
    ?>
    
    <?php if ($pageposts): ?>
    <ul>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post);?>
    
    	<li><a href="<?php the_permalink() ?>"><b><?php the_title(); ?></b><br />
    	<?php
    		//Get start date value
    		$startdate_values = get_post_custom_values('startdatum');
    		$startdate_values_copy = $startdate_values;
    		$start = 0;
    
    		if (strlen($dateIndexes[$post->ID]) > 0 && $dateIndexes[$post->ID]>=0)
    		{
    			$start = $dateIndexes[$post->ID]+1;
    		}
    
    		$location_values = get_post_custom_values('ort');
    
    		sort($startdate_values);
    		for ($i=$start; $i< 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>=$today)
    			{
    				//Print out
    				echo strftime('%e %b, %Y', $nextdate);
    
    				$index = -1;
    				//Find the right index before sortig
    				for ($j=$start; $j< count($startdate_values_copy); $j++)
    				{
    					if ($startdate_values_copy[$j] == $startdate_values[$i])
    					{
    						$index = $j;
    						break;
    					}
    				}
    
    				//Put into an array
    				$dateIndexes[$post->ID] = $i;
    
    				//If found, break the loop
    				break;
    			}
    		}
    	?></a></li>
    
    <?php endforeach; ?>
    </ul>
    <?php else : ?>
    <p>No upcoming events</p>
    <?php endif; ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Multiple meta_value for same page’ is closed to new replies.