Coding Help :: PHP & Wordpress using categories and custom fields referencing dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mike King

    Coding Help :: PHP & Wordpress using categories and custom fields referencing dates

    site: http://www.pleinairbc. com/event-listings/

    I have some code to help filter posted events based on location name, which is defined with a sub category under the main category 'location' (id 35). The location name in the drop box will only display if the end date is the same or less than todays date. The end date is defined in the 'custom field' area when posting via worpress 3.x as the variable 'endDate' looking like 2010/10/08 (October 8, 2010)

    All location names (Langley, Coquitlam, etc)are parented to the category 'location'.

    The issue is that two 'locations' (Langley and Salt Spring Island) for events are showing up in the list which have come and gone. And one (Rocky Mountains) is not showing up at all.

    My code is below. Please help me out.

    Cheers,

    Michael


    Code:
    <a name="results"></a>
    
    <div style="float: none;">
    
    <div  style="float: left;">
    
    <select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> 
    
    <option value=""><?php echo attribute_escape(__('Select Type')); ?></option> 
    
    <option value="/?page_id=284">All Events</option>
    
    <option value="/?page_id=284&eventtype=12">Free</option>
    
    <option value="/?page_id=284&eventtype=52">$ Low Cost <$150</option>
    
    <option value="/?page_id=284&eventtype=13">$$ Paid Workshops >$150</option>
    
    </select><br /><br />
    
    </div><div style="float: left;">
    
    <div  style="float: left;">&nbsp;&nbsp;&nbsp;</div>
    
    <?php
    
    	query_posts('cat=12,13,52&meta_key=endDate&meta_compare=>=meta_value= '.$todaysDate.' &orderby=meta_value&order=ASC');
    
    	$the_post_cats = array();
    
    		if (have_posts()) : while (have_posts()) : the_post();
    
    	$the_post_cats = array_merge($the_post_cats, wp_get_post_categories($post->ID)); 
    
    ?>
    
    		<?php endwhile; endif; ?>
    
    <select style="float: left;" name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> 
    
     <option value=""><?php echo attribute_escape(__('Select Location')); ?></option> 
    
     <option value="/?page_id=284">All Locations</option>
    
    <?php wp_list_categories( $args ); ?> 
    
    
     <?php 
    
      $categories= get_categories('child_of=35');
    
      foreach ($categories as $cat) {
    
    
    	$option = '<option value="/?page_id=284&location='.$cat->term_id.'">';
    
    	$option .= $cat->cat_name;
    
    	$option .= '</option>';
    
    	if(!in_array($cat->term_id , $the_post_cats)) echo $option;
    
      }
    
    ?>
    
    </select>
    
    </div></div>
    
    <br clear="all" /><br />
    
    <?php $todaysDate = date('Y/m/d'); //echo $todaysDate;
    
    if(isset($_GET["eventtype"])) {$eventtype= $_GET["eventtype"];} else {$eventtype = "12,13,52";}
    
    if(isset($_GET["location"])) {$eventtype= $_GET["location"]; echo 'Showing results for the following location:<br /><span style="color: orange; font-weight: bold;">' . get_cat_name( $eventtype) . '</span><br /><br />';}
    
    	query_posts('showposts=20&cat=' . $eventtype . '&meta_key=endDate&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
    
    $the_dimension = 0 ; $posts2print = array();
    
    		if (have_posts()) : while (have_posts()) : the_post();
    
    	$sdate = get_post_custom_values('startDate', $post->ID);
    
    	$edate = get_post_custom_values('endDate', $post->ID);
    	if($sdate == '') $sdate = $edate;
    
            $the_post_title = $post->post_title;
    
    	$the_post_permalink = get_permalink();
    
    	$typeofevent = '';
    
    	if (in_category('12')) $typeofevent = ' <span style="font-size: small; color: #2C9B2E;">Free Event!</span>';
    
            if (in_category('52')) $typeofevent = ' <span style="font-size: small; color: #1194C3;">Low Cost Event</span>';
    
    	if (in_category('13')) $typeofevent = ' <span style="font-size: small; color: #C39111;">Paid Workshop</span>';
    
    	$locationregion = get_post_custom_values('region', $post->ID);
    
    $cur_post = array($the_dimension => array("sdate" => $sdate[0], "edate" => $edate[0], "ptitle" => $the_post_title, "ppermalink" => $the_post_permalink , "tevent" => $typeofevent , "lregion" => $locationregion[0]));
    
    
    $posts2print = $posts2print + $cur_post; $the_dimension++;?>
    
    <?php endwhile; endif; ?>
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Hi, when you say list are you referring to the drop down for locations or the list of events below? I just visited your site and I only see two events listed both for west vancouver.

    Comment

    • mrking
      New Member
      • Feb 2008
      • 28

      #3
      I am referring to the list in the drop down for 'locations'.

      Yes, there are two events listed for West Vancouver. That location should be the only one showing up in the 'locations' drop down menu.

      Comment

      • JKing
        Recognized Expert Top Contributor
        • Jun 2007
        • 1206

        #4
        On line 61 there is an if statement inside a for each loop. This is where you determine what options should be printed.

        Try printing out $the_post_cats and verifying which category ids are in it and compare them to the category ids being printed to the drop down.

        If the results aren't what you expect you may need to rework your if statement logic.

        Comment

        Working...