Imagine I have a site set up with pages in this kind of hierarchy..

 -Volvo 850 
  -Volvo 850 tech spec 
  -Volvo 850 pictures
 -Porsche 911 
  -Porsche 911 tech spec
  -Porsche 911 pictures
 -other cars etc

Now you can see that I have a bunch of Cars. On each of the car pages (Volvo 850, Porsche 911) I have some Custom Fields where I have entered various data about the Car (by the way, I'm using the Custom Field Template plugin to make entry of this data easy in wp-admin). These fields are displayed on the page. (the fields are not entered on the tech spec and pictures pages)

What I would like to do is display a list on the cars on the Home page (below), which gets the data from the Custom Fields. What would be the best way to (a) access this custom field data (which can change when new cars are added or edited), and (b) display the data in a certain order (for instance by top speed)..?

|   Car              |   Top Speed    |
| Porsche 911        |   200          |
| Audi 444           |   180          |
| Volvo 840          |   160          |

I guess the pseudo-code is

* find all pages which are an immediate descendant of the Cars page (not the tech spec and pictures)
* get the custom data from these pages
* display data in Top Speed order

1 Answer 1


This one might get you started (this one is for the size as you can see since i do value 1 times value 2) (just to give an example of more advanced queries)

 global $edl_global_join;
 global $edl_global_orderby;
 global $wp_query;

 function edl_posts_join ($join) {
   global $edl_global_join;
   if ($edl_global_join) $join .= " $edl_global_join";
   return $join;

 function edl_posts_orderby ($orderby) {
  global $edl_global_orderby;
  if ($edl_global_orderby) $orderby = $edl_global_orderby;
  return $orderby;


 $edl_global_join = 
 "JOIN $wpdb->postmeta meta1 ON (meta1.post_id = $wpdb->posts.ID AND meta1.meta_key = 'TOPSPEED')" .
 "JOIN $wpdb->postmeta meta2 ON (meta2.post_id = $wpdb->posts.ID AND meta2.meta_key = 'ANOTHER_THING')";
 $edl_global_orderby = " meta1.meta_value * meta2.meta_value DESC";

 $wp_query = new WP_Query($args);

and then just run the loop I wrote a "CAR class" which among others displays meta fields such as:


which is actually the following method in that class:

// specific display for size overviews
function display_meta_size()
    <table width="100%">

where the method GetValuesFromWP() is from the wp metadata class :

// get the values stored in WordPress
function GetValuesFromWP() {
    global $post;
    $custom = get_post_custom($post->ID);

    foreach ($this->mArrMetaDataFields as $str_meta_data_field)
        $this->MetaDataWpValues[$str_meta_data_field] = 
    $this->MetaDataWpValues['SPECIAL'] = $custom['SPECIAL'][0];

(so in the join function totally above add the page selection(s))

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.