• Resolved bartleriche

    (@bartleriche)


    Hi!

    I’m trying to display a list with all the posts of a post type called ‘agenda_item’, in descending order. The hard part is that they need to be ordered by a date which is stored in a custom field called ‘agenda-datum’. And the format of this date is dd-mm-yyyy. I can’t wrap my head around this, maybe you guys can help me out, thanks!

    This is what I got so far:

    <?php
    query_posts( array (
    	'post_type' => 'agenda_item',
    	'meta_key' => 'agenda-datum',
    	'orderby' => 'meta_value',
    	'order' => 'DESC'
    ) );
    
    while ( have_posts() ) : the_post();
    	//more_fields(); Echoes the value of a given custom field key
    	more_fields("agenda-datum");
    endwhile;
    
    wp_reset_query();
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m not super-familiar with the loop yet…

    But, can you try this?

    query_posts( array (
    	'post_type' => 'agenda_item',
    	'meta_key' => more_fields("agenda-datum"),
    	'orderby' => 'meta_value',
    	'order' => 'DESC'
    ) );

    That should work, but the dates have to be stored in the database as YYYYMMDD, like

    20130809

    The SQL query won’t see dates, but rather string values, so you have to use structure the dates like that.

    Then:

    <?php
    while(have_posts()) : the_post();
      // get the date
      $date= more_fields('agenda-datum');
      // convert to Unix timestamp, then reformat, then print result
      echo date(strtotime('M d Y', $date));
      // above prints ex. Aug 09 2013

    Thread Starter bartleriche

    (@bartleriche)

    With my first try I got this:
    13-3-2014
    1-12-2014
    13-8-2013
    1-12-2013

    With your hint I got this:
    13-3-2014
    1-12-2014
    1-12-2013
    13-8-2013

    While I want to get this:
    1-12-2014
    13-3-2014
    1-12-2013
    13-8-2013

    Thread Starter bartleriche

    (@bartleriche)

    Yes! Christian1012 you sir are a gentlemen, with your hint I got it to work. I ended up storing the dates in the database the way you said, and it worked fine. Thanks a lot!

    Hi there i saw the post and it looks like the seems to offer the solution but I still can’t figure it out. I’m having the same problem here!

    Before reading the post i had my code as below

    <?php
       $post_meta_data = get_post_custom($post->ID);
    
         if( !empty($post_meta_data['SHOM_available_date'][0]) ) {
    
         $av_date = $post_meta_data['SHOM_available_date'][0];
    
        echo '<small>&nbsp;Available After&nbsp;'.$av_date.'&nbsp;</small>';
    	}
    ?>

    And I made the changes to store the field as YYYYMMDD on custom meta_box
    and on the DB they are store as YYYYMMDD after changed the above to:

    <?php
     $post_meta_data = get_post_custom($post->ID);
    
        if( !empty($post_meta_data['SHOM_available_date'][0]) ) {
    
         $av_date = $post_meta_data['SHOM_available_date'][0];
    
         echo date(strtotime('M d Y', $av_date));;
        }
    ?>

    After changing it doesn’t echo at all!!

    Does anyone have any idea?

    Resolved by changing to this:

    <?php
          $post_meta_data = get_post_custom($post->ID);
          if( !empty($post_meta_data['SHOM_available_date'][0]) ) {
          $av_date = $post_meta_data['SHOM_available_date'][0];
          echo '<small> Available After&nbsp;';
          echo date("M d Y", $av_date);
          echo '</small>';
          }
    ?>

    Thnx for the post to all

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Order by custom field date’ is closed to new replies.