I'm not new to wordpress, but I'm not very good at PHP. I hope someone can help me, since I already tried to search for a solution for over 5 hours with no luck.

So I have an archive.php page where I want to to sort the posts by 3 meta keys. They all have numeric values. These are the names of the meta fields: "vorstellung-filmdatum" (The date), "vorstellung-uhrzeit-stunde" (The hour) and "vorstellung-uhrzeit-minute" (The Minute).

At this moment I'm using the following code, but it only sorts by the last meta-key ("vorstellung-uhrzeit-minute" in this case).

global $wp_query;
$args = array_merge( $wp_query->query, array( 'posts_per_page' => 12, 'meta_key' => 'vorstellung-filmdatum', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'meta_key' => 'vorstellung-uhrzeit-stunde', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'meta_key' => 'vorstellung-uhrzeit-minute', 'orderby' => 'meta_value_num', 'order' => 'ASC', ) );
query_posts( $args );
while (have_posts()) : the_post(); ?>

How can I change this to be sorted by all three meta keys?

I want it to sort the posts like this:

-- Date 1

--- Hour 1

------ Minute 1

------ Minute 2

--- Hour 2

------ Minute 1

------ Minute 2

-- Date 2

--- Hour 1

------ Minute 1


Can someone help me?

  • if it´s not too late, I´d add another meta with exact time and date in it´s value and sorted posts by that one. It also saves you multiple post meta per post as you have all necessary information in a single meta value. Commented Jul 22, 2014 at 14:56

1 Answer 1


I think the best way would be to store the complete date-time in one meta field, saving it as an timestamp in the metafield and converting it by php datetime object functions for displaying.

Step 1)
Create a Metabox with a text input for entering the date + time. You can find a good introduction to meta boxes here.

Step 1.5) if you want it to be easily usable, you can add a jquery plugin for date/time picking in the post edit screen, like this one.

Step 2)
in the myplugin_meta_box_callback function, convert the meta value $value into your desired display format using the php datetime functions like this:

   $date = date_create($value);
   $value = date_format($date, 'Y-m-d H:i');

Step 3)
In the save_metabox action, convert the date/time-Value by the php strtotime function to an unix timestamp like this:


 $my_data = sanitize_text_field( $_POST['myplugin_new_field'] );


 $my_data = strtotime($_POST['myplugin_new_field']);

Step 4)
In your archive, give your query the orderby "meta_value_num", ordering it by numerical meta values of your custom field

Step 5)
Do NOT use query_posts.

Step 6)
Check in your while loop if the day of the current metavalue is higher than the day of the last metavalue

$day = 0;
while $query->have_post {
    $newday = get_post_meta(get_the_ID(),'NAME_OF_METAFIELD',true);
    $newdate = date_create($newday);
    $newday = date_format($newdate,'d');
    if(!($newday==$day)) {
        $day = $newday;
        echo "Date:".date_format($newdate,'Y-m-d');
    echo "Time:".date_format($newdate,'H:i');
    //All the other stuff that needs to be displayed

Step 7)
Done (more or less)... Do some tweaking

I didn't include the if-then stuff for the hour, but I think you can figure it out^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.