Support » Fixing WordPress » Sort posts by the day rather than chronological

  • Resolved canheread7

    (@canheread7)


    I am building a simple birthday plugin for a client. It is a custom post type that takes the title as the name of the person and the post date as the date of birth. It then filters the posts to show only the birthdays of the current month. My problem is the posts are being sorted chronologically rather than by the day of the month for example: if John Smith was born 1/17/1968 he would show up BEFORE Luise Reynolds who is born 1/3/1992 where I would prefer the list be sorted by the day, that is ‘3’ coming before ’17’ here is my code below.

    $args = array(
    						'post_type' => 'birthdays',
    						'orderby' => 'date',
    						'order' => 'ASC'
    						);
    
    					$birthdays = new WP_Query($args);
    
    					if($birthdays->have_posts()){
    						while ($birthdays->have_posts()){
    							$birthdays->the_post();
    
    							$current_date = date('m');
    							$post_date = get_the_time('m');
    							$age =  date('Y') - get_the_time('Y');
    
    							if($current_date==$post_date){
    								?>
    								<div style="padding:50px">
    								<h2 style="font-size:24pt;"><?php the_title()?></h2>
    								<h3 style="font-size:18pt;"><em>is turning <?php echo $age;?> on the <?php echo get_the_time(jS);?></em></h3>
    								</div>
    								<?php
    							}
    						}
    					}else{
    						echo 'No birthdays entered yet!';
    					}

    I’m sort of new to making plugins so any help would be much appreciated! Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • ‘order’ can be ascending (ASC) or descending (DESC)

    http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Thread Starter canheread7

    (@canheread7)

    Thanks for your fast reply! Unfortunately I don’t think that will solve my problem as wordpress is sorting the posts from oldest to newest in the scope of all time in that the post from 1968 comes before the one from 1992, but I would like the posts only to be sorted within the scope of the month meaning that a post on the 5th day will always come before a post on the 17th day regardless whether or not the post on the 17th was posted a hundred years before the other one.

    Moderator keesiemeijer

    (@keesiemeijer)

    Maybe if you query for posts of the current month:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'monthnum' => date('m'),
      'orderby' => 'date',
      'order' => 'ASC',
      'paged' => $paged
    );
    $birthdays = new WP_Query($args);

    http://codex.wordpress.org/Function_Reference/WP_Query#Time_Parameters

    Thread Starter canheread7

    (@canheread7)

    Thanks for the input! Your code definitely cleans up my if statement later on but it seems that it is still sorting the posts by the year. Is there any way to possibly automatically set the menu order to the day of the month and then sort the posts by the menu order? That sounds messy in my mind and don’t really know how I would go about doing that.

    Thanks again!

    You can do what you want by using filters to alter the query. The filters referenced in the code below can be found here: http://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/

    The code below should be very close to what you need.

    global $mam_global_fields;
    $mam_global_fields = ", SUBSTRING($wpdb->posts.post_date, 6, 2) AS birthmonth";
    
    global $mam_global_orderby;
    $mam_global_orderby = " birthmonth, $wpdb->posts.post_date, $wpdb->posts.post_title";
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'monthnum' => date('m'),
      'orderby' => 'date',
      'order' => 'ASC',
      'paged' => $paged,
      'ignore_sticky_posts' => 1,  // stickies will show at the top unless ignored
    );
    $birthdays = new WP_Query($args);
    $mam_global_orderby = '';  // Clear filter
    $mam_global_fields = '';
    
    if ($birthdays->have_posts()) {
       while ($birthdays->have_posts()) {
          $birthdays->the_post();
          echo '<br />';the_time('Y-m-d'); echo " &nbsp; BMONTH:$post->birthmonth &nbsp; ";
          the_title();
       }
    }

    The output in the Loop is just for my own debugging – change it to what you want.

    Thread Starter canheread7

    (@canheread7)

    Thanks vtxyzzy! That actually worked great! I can imagine almost an endless amount of possibilities for filters! great!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sort posts by the day rather than chronological’ is closed to new replies.