WordPress.org

Ready to get started?Download WordPress

Forums

Query doesnt seem to be sorting on year (8 posts)

  1. phillip@exsede.com
    Member
    Posted 1 year ago #

    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(); ?>
  2. esmi
    Forum Moderator
    Posted 1 year ago #

    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.

  3. phillip@exsede.com
    Member
    Posted 1 year ago #

    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)

  4. esmi
    Forum Moderator
    Posted 1 year ago #

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

  5. phillip@exsede.com
    Member
    Posted 1 year ago #

    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?

  6. bcworkz
    Member
    Posted 1 year ago #

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

  7. phillip@exsede.com
    Member
    Posted 1 year ago #

    Where would I insert this in the query?

  8. bcworkz
    Member
    Posted 1 year ago #

    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];

Topic Closed

This topic has been closed to new replies.

About this Topic