stirling web design and development

Order Custom Fields Correctly in WordPress

Need your post's custom fields to output in the order set in the admin page .....

This might be reinventing the wheel but i could not get my custom fields to output in the correct order in WordPress. Custom fields are really powerful and can be used for a tonne of different things in my case I was using it to store product values for an online shop which integrated into PayPal. I just couldn’t see a simple way to order the custom fields in the same way that they had been ordered in the post??

So i created this little function which will hopefully help someone out there to order their custom fields correctly. fire it into your main loop and it should work like a charm.

You can fiddle about with it to your hearts content, you might want to order it slightly differently or change the list classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function getPostMeta(){
   global $wpdb;
   $query = "SELECT * FROM " . $wpdb->postmeta . " WHERE  post_id=\"".$post->ID."\" ORDER BY meta_id ASC";
   $post_data = $wpdb->get_results($query);
   $array = array();
      if(!empty($post_data))
      {
      $out.="<ul>";
      foreach($post_data as $p)
         {
         $out.="<p><span class='post-meta-key'>".$p->meta_key." :</span><span class='meta-value'>".$p->meta_value."</span></p>";
         }
      $out.="</ul>";
      }
return $out;
}