Leave a Reply

2 comments

  1. Typically in WordPress you would never go use database calls/SQL Calls (your second question)

    (on your first question: how to add multiple metadata values (and do the wildest things with them)) use kind the following for additional entries in your Query, the join and orderby are used here for other possible relations between meta keys. In the example you see that I order on the A*B values of the keys. Just copy and paste and adjust to your liking. (note that apart from join and orderby there are more options to play with in case of even more advanced scenarios).

     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;
     }
    
     add_filter('posts_join','edl_posts_join');
     add_filter('posts_orderby','edl_posts_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);
    

    If you want to then show certain fields based on calculations of the meta data you can use e.g.:

     $car->display_meta_size();
    

    which is actually the following method in that class:

        //
    // specific display for size overviews
    //
    function display_meta_size()
    {
        $this->mMetaData->GetValuesFromWP();
        ?>
        <table width="100%">
        <?php   
        $this->mMetaData->ShowIcon();
        $this->mMetaData->ShowSize();
        ?>
        </table>
        <?php
    } 
    

    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] = 
                        $custom[$str_meta_data_field][0];
        }
        $this->MetaDataWpValues['SPECIAL'] = $custom['SPECIAL'][0];
    }
    

    (so in the join function totally above do the stuff you want).

    see also: Getting Custom Field data from a page hierarchy