Title: Optimize Query WordPress With Post Meta
Last modified: August 20, 2016

---

# Optimize Query WordPress With Post Meta

 *  [babyskill](https://wordpress.org/support/users/babyskill/)
 * (@babyskill)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/optimize-query-wordpress-with-post-meta/)
 * Hi every body,
 * When we are having problems because wordpress uses too much system resources,
   I have a way to reduce the query load:
 * **Join Post Meta**: We will use often post_meta should work first of all we have
   to join this table.
 *     ```
       function kk_posts_join($join) {
           global $wpdb;
           $join .= " LEFT JOIN $wpdb->postmeta metapic ON ($wpdb->posts.ID = metapic.post_id AND metapic.meta_key = 'thumbs') LEFT JOIN $wpdb->posts pic ON (pic.ID = metapic.meta_value)";
           $join .= " LEFT JOIN $wpdb->postmeta metacon ON ($wpdb->posts.ID = metacon.post_id AND metacon.meta_key = 'icon')";
           $join .= " LEFT JOIN $wpdb->postmeta metaview ON ($wpdb->posts.ID = metaview.post_id AND metaview.meta_key = 'views')";
           return $join;
       }
       ```
   
 * `add_filter('posts_join', 'kk_posts_join');`
 * And get some field:
 *     ```
       function kk_posts_fields($fields) {
           $fields .= ',pic.guid AS thumbs,metacon.meta_value AS icon, metaview.meta_value AS view';
           return $fields;
       }
       ```
   
 * `add_filter('posts_fields', 'kk_posts_fields');`
 * **Fields:**
    Default wordpress wp_posts. * When retrieving the data, we will 
   take the necessary fields.
 * Somethink like there:
 *     ```
       function kk_posts_fields($fields) {
           global $wpdb
           $fields = "$wpdb->posts.post_title,$wpdb->posts.post_name,$wpdb->posts.post_content,$wpdb->posts.ID,$wpdb->posts.post_date,pic.guid AS thumbs,metacon.meta_value AS icon, metaview.meta_value AS view";
           return $fields;
       }
       ```
   
 * We have reduced a lot of query of wordpress then, wish you success. Sorry for
   my English.
 * _[sig moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome)]_

The topic ‘Optimize Query WordPress With Post Meta’ is closed to new replies.

## Tags

 * [post_meta](https://wordpress.org/support/topic-tag/post_meta/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [babyskill](https://wordpress.org/support/users/babyskill/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/optimize-query-wordpress-with-post-meta/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
