Support » Plugins and Hacks » Hacks » Query doesnt seem to be sorting on year

  • OK, here’s my issue. I have a website that has “Recent” as well as “Upcoming” events on a “Company News” page. These events are custom posts and each post has a custom field of Date (date format is mm/dd/yyyy). The code I am using to create the two event types on this page isn’t taking the year (yyyy) into consideration for some reason. Is it obvious to anyone out there as to why this isn’t working? Here is the php content:

    <?php
    /**
     * Template Name: Events
     */
    
    get_header(); ?>
    <?php
      $record_limit = 10;
      $upcoming_count = 1;
    	$recent_count = 1;
    ?>
    
     	<div id="inner-banner">
                    	<!--banner-sub start here-->
    				  <?php
                        if (has_post_thumbnail()){
                       ?>
                            <div id="banner-sub">
                       <?php the_post_thumbnail('related-post-thumbnail',array('title' => "")); ?>
                            </div>
                      <?php
                        }
                       else{
                       ?>
                         <div id="banner-sub">
                         <img src="<?php echo get_bloginfo('template_url')?>/images/inner-banner.jpg" alt="inner banner" title="inner banner" border="0"/>
                        </div>
                       <?php
                        }
                       ?>
    					<!--banner-sub end here-->
                    </div>
            <!--banner end here-->   
    
            <div id="center_content_wrapper">
            <div class="breadcrumbs">
    				<?php if(function_exists('bcn_display'))
                    {
                        bcn_display();
                    }?>
                </div>
            		<?php the_post(); ?>
    
    				<?php get_template_part( 'content', 'page' ); ?>	
    
                    <!--company news-->
            		<div id="company_news_wrapper">
                     <h2>Company News</h2>
    				 <?php
    		  	  // The Query
    				$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    				  $args=array(
             			'post_type' => 'companynews', 'posts_per_page' => 10,
             			'paged' => $paged
            			 );
    				query_posts($args);
    
    				// The Loop
    				while ( have_posts() ) : the_post();
    				/**********LOOP CONTENT STARTS HERE***/
    
    				?>
                    	<div class="news_area">
    						<div class="news_date"><?php  $arrDate = get_post_custom_values("Date", $post->ID); echo $date = $arrDate [0]; ?></div>
                        	<div class="news_heading"><a href="<?php the_permalink(); ?>"  rel="bookmark"><?php the_title(); ?></a></div>
                        </div>
    
                      <?php
    
    				/**********LOOP CONTENT ENDS HERE***/
    				endwhile;
    
    				// Reset Query
    
    				?>
                 </div>
             <?php wp_reset_query();?>
    
                      <!--upcoming news-->
                    <div id="upcoming_news_wrapper">
                    <h2>Upcoming Events</h2>
    				 <?php
    		  	  // The Query
    				$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    				  $args = array(
             			'post_type' => 'events', 'posts_per_page' => 11,
             			'paged' => $paged,
    							'meta_key' => 'date',
    						  'meta_compare' => '>=',
    						  'meta_value' => date("m/d/Y"),
    						  'orderby' => 'meta_value',
    						  'order' => 'DESC'
            			 );
    
    				$upcoming_records = query_posts($args);			
    
    				// The Loop
    				while ( have_posts() && $upcoming_count <= 10) : the_post();
    				/**********LOOP CONTENT STARTS HERE***/
    
    				?>
                    	<div class="news_area">
    						<div class="news_date"><?php  $arrDate = get_post_custom_values("Date", $post->ID); echo $date = $arrDate [0]; ?></div>
                        	<div class="news_heading"><a href="<?php the_permalink(); ?>"  rel="bookmark"><?php the_title(); ?></a></div>
                        </div>
    
                      <?php
    										$upcoming_count++;
    				/**********LOOP CONTENT ENDS HERE***/
    				endwhile;
    
    				// Reset Query
    
    				?>
    				<?php if (count($upcoming_records) > $record_limit ): ?>
    					<div class="news_area">
    						<div class="news_date">
    							<a href="<?php print get_bloginfo('url'); ?>/allevents?type=upcoming" rel="bookmark" style="text-decoration: none;"><strong>Read More...</strong></a>
    						</div>
              </div>
    				<?php endif; ?>
                 </div>
                     <?php wp_reset_query(); ?>
    
            		<!--past news-->
            		<div id="news_wrapper">
                     <h2>Recent Events</h2>
    				 <?php
    		  	  // The Query
    				$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    				  $args=array(
             			'post_type' => 'events', 'posts_per_page' => 11,
             			'paged' => $paged,
    							'meta_key' => 'date',
    						  'meta_compare' => '<=',
    						  'meta_value' => date("m/d/Y"),
    						  'orderby' => 'meta_value',
    						  'order' => 'DESC'
            			 );
    				$recent_records = query_posts($args);
    
    				// The Loop
    				while ( have_posts() && $recent_count <= 10) : the_post();
    				/**********LOOP CONTENT STARTS HERE***/
    
    				?>
                    	<div class="news_area">
    						<div class="news_date"><?php  $arrDate = get_post_custom_values("Date", $post->ID); echo $date = $arrDate [0]; ?></div>
                        	<div class="news_heading"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></div>
                        </div>
    
                      <?php $recent_count++;
    
    				/**********LOOP CONTENT ENDS HERE***/
    				endwhile;
    
    				// Reset Query
    
    				?>
    				<?php if (count($recent_records) > $record_limit): ?>
    					<div class="news_area">
    						<div class="news_date">
    							<a href="<?php print get_bloginfo('url'); ?>/allevents?type=recent" rel="bookmark" style="text-decoration: none;"><strong>Read More...</strong></a>
    						</div>
              </div>
    				<?php endif; ?>
                 </div>
                     <?php wp_reset_query(); ?>
    
                    <?php if ( ! dynamic_sidebar( 'Presskit ' ) ) : ?>
       				 <?php endif;?> 
    
    		</div>
    
    <?php get_footer(); ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • esmi

    (@esmi)

    Forum Moderator

    each post has a custom field of Date (date format is mm/dd/yyyy)

    That’s your problem. In order to work effectively as dates stored in a string format, the dates need to be in yyyy/mm/dd format.

    Can the Date custom field be formatted to accept that format and where do I do this? (It is formatted for the m/d/Y by default)

    esmi

    (@esmi)

    Forum Moderator

    Custom fields are just strings. You can use any format you want.

    That worked great! Thank you so much. Is there a line of code (or two) I could put into the query that would display the format as m/d/Y now that I have it sorting correctly?

    Moderator bcworkz

    (@bcworkz)

    echo date('m/d/Y', strtotime($arrDate[0]));

    Where would I insert this in the query?

    Moderator bcworkz

    (@bcworkz)

    Sorry, I did not read your question carefully enough. You can’t do this from a query. The query specifies what information to return and how it’s organized, but it cannot change the actual data as it is read. The data is typically Y/m/d for proper sorting. To change this around for us humans, you need to do this in PHP. Specifically, in the loop wherever you want the date to show. I see this replacing echo $date = $arrDate [0];

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Query doesnt seem to be sorting on year’ is closed to new replies.