Title: have_posts and orderby
Last modified: August 21, 2016

---

# have_posts and orderby

 *  [Shellanza](https://wordpress.org/support/users/shellanza/)
 * (@shellanza)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/have_posts-and-orderby/)
 * Hi,
    I’m trying to modify the archive.php file because I need to order the results
   from the query in a datetime way…
 * The Loop start with the typical:
 *     ```
       <?php
       while ( $the_query->have_posts() ) : $the_query->the_post();
       ?>
       ```
   
 * So I read around and people in previous posts suggested to use this before:
 * `<?php query_posts('meta_key=data&orderby=meta_key&order=DESC');?>`
 * But doesn’t work.
 * The table fields where he’s grabbing data with mysql is something like this:
 * `INSERT INTO`wp_postmeta`(`meta_id`,`post_id`,`meta_key`,`meta_value`) VALUES
   (
   86074, 15099, ‘orario_corsi’, ‘15.00 – 18.00’), (86075, 15099, ‘tipo_corso’, ‘
   Demo gratuita’), (86076, 15099, ‘punti_vendita’, ‘a:1:{i:0;s:8:”Collegno”;}’),(
   86077, 15099, ‘data’, ‘20130930’), (86078, 15099, ‘prezzo_corso’, ”), (86079,
   15099, ‘_wp_page_template’, ‘default’), (86080, 15099, ‘_yoast_wpseo_linkdex’,’
   43’), (86081, 15099, ‘_edit_lock’, ‘1378307873:1’), (86082, 15099, ‘_thumbnail_id’,‘
   14271’), (86083, 15099, ‘_edit_last’, ‘1’), (86084, 15099, ‘novità’, ‘0’), (86085,
   15099, ‘_novità’, ‘field_51a49e93b7613’), (86086, 15099, ‘_punti_vendita’, ‘field_51891cfde30f4’),(
   86087, 15099, ‘_data’, ‘field_518cbb149ef68’), (86088, 15099, ‘_orario_corsi’,‘
   field_521f68b550fbd’), (86089, 15099, ‘_prezzo_corso’, ‘field_521f6a53c2ec6’),(
   86090, 15099, ‘_tipo_corso’, ‘field_521f68fbb78a5’), (86091, 15099, ‘gallery’,‘
   0’), (86092, 15099, ‘_gallery’, ‘field_517b9480b9e6d’), (86093, 15099, ‘documenti’,‘
   0’), (86094, 15099, ‘_documenti’, ‘field_517b94ece23db’), (86095, 15099, ‘_expiration-
   date’, ‘1380578400’), (86096, 15099, ‘_expiration-date-options’, ‘a:2:{s:10:”
   expireType”;s:5:”draft”;s:2:”id”;i:15099;}’), (86097, 15099, ‘_expiration-date-
   status’, ‘saved’), (86098, 15099, ‘_yoast_wpseo_focuskw’, ‘medium materici’),(
   86099, 15099, ‘_yoast_wpseo_title’, ‘Rilievi d”effetto – Viridea’), (86100, 15099,‘
   _yoast_wpseo_metadesc’, ‘Usiamo i medium materici per effetti tridimensionali.’),(
   86101, 15099, ‘_yoast_wpseo_meta-robots-noindex’, ‘0’), (86102, 15099, ‘_yoast_wpseo_meta-
   robots-nofollow’, ‘0’), (86103, 15099, ‘_yoast_wpseo_meta-robots-adv’, ‘none’),(
   86104, 15099, ‘_yoast_wpseo_sitemap-include’, ‘-‘), (86105, 15099, ‘_yoast_wpseo_sitemap-
   prio’, ‘-‘), (86106, 15099, ‘_yoast_wpseo_sitemap-html-include’, ‘-‘), (86107,
   15099, ‘_yoast_wpseo_canonical’, ”), (86108, 15099, ‘_yoast_wpseo_redirect’, ”),(
   86109, 15099, ‘_yoast_wpseo_opengraph-description’, ”), (86110, 15099, ‘_yoast_wpseo_google-
   plus-description’, ”), (86111, 15099, ‘_dp_original’, ‘14137’),`
 * Need help 🙂
 * Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/have_posts-and-orderby/#post-4152088)
 * what theme are you using?
 * what is the full code of the template?
 *  Thread Starter [Shellanza](https://wordpress.org/support/users/shellanza/)
 * (@shellanza)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/have_posts-and-orderby/#post-4152100)
 * It’s a theme made by scretch.
    The problem seem to be connected to the “Advanced
   Custom Field” plugin that give the possibility to add extra field. I’m afraid
   these fields are not involved in the typcal ordering.
 * I read this article but I’m still stuck!
 * [http://halgatewood.com/orderby-advanced-custom-field-in-query-posts/](http://halgatewood.com/orderby-advanced-custom-field-in-query-posts/)
 *  Thread Starter [Shellanza](https://wordpress.org/support/users/shellanza/)
 * (@shellanza)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/have_posts-and-orderby/#post-4152113)
 * I think I solved.
    I hopefully find an article here:
 * [http://www.advancedcustomfields.com/resources/how-to/orde-posts-by-custom-fields/](http://www.advancedcustomfields.com/resources/how-to/orde-posts-by-custom-fields/)
 * Thanks for your help in any case. I hope this would be usefull for the next one.
 * Cheers.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘have_posts and orderby’ is closed to new replies.

## Tags

 * [have_post](https://wordpress.org/support/topic-tag/have_post/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [Shellanza](https://wordpress.org/support/users/shellanza/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/have_posts-and-orderby/#post-4152113)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
