WordPress sort posts by custom field

Posted on 2012-09-13
Last Modified: 2012-09-15
Hello Experts,

I am trying to sort WP posts by a custom filed which is a date field.
For some reason it does not work. Can anyone help please?

$parent_id = $posts[0]->ID;
  'post_parent' => $parent_id,
  'post_type' => 'page',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1 ,
  'order'=> 'ASC',

$my_query = null;
$my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post();
           $event_date = get_post_meta($post->ID, 'event_date', true);
           $display_event_date = date('dS F Y', strtotime(get_post_meta($post->ID, "event_date", true)));  
          $event_title = get_post_meta($post->ID, 'event_title', true); 

Open in new window

Question by:Refael
    LVL 70

    Expert Comment

    by:Jason C. Levine
    How is the date stored in meta?  As a string e.g. "01/01/2012"?

    Author Comment

    Hi jason1178,

    Thank you. The format is stored like so "4/20/2012".

    Author Comment

    Hi jason1178,
    I replied to your question yesterdays . Is there a solution? Can you help?
    LVL 70

    Expert Comment

    by:Jason C. Levine
    That format can't be sorted like a string would be and if these are stored in meta, they are simple strings.

    10/1/2099 would come before 4/20/2012 because of the leading 1.  You either need to format the dates differently so they sort better or switch up what you are doing so you write dates to a date data type.

    Author Comment

    Hi jason1178,

    Can you give me an example? is there a way to print the date in this format but let it sort in the format it stores in the database?
    LVL 70

    Accepted Solution

    Switch to 10 digit dates.  


    Author Closing Comment

    Hi Jason1178
    Thank you. i simply changed the format to "d.m.y" and it solved it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now