Title: Combining multiple custom fields into single query?
Last modified: August 22, 2016

---

# Combining multiple custom fields into single query?

 *  [purrdesign](https://wordpress.org/support/users/purrdesign/)
 * (@purrdesign)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/combining-multiple-custom-fields-into-single-query/)
 * I have a series of 3 custom fields that I am calling for each post. On pages 
   where I have 10+ posts displaying, it is adding a lot of extra database queries.
   I’m hoping there is some way to combine the 3 queries into a single one, to hopefully
   reduce the load on the server. Here is what I have now, within the post loop 
   so the 3 fields show conditionally on each post (must be conditional because 
   the span shouldn’t display at all if the field is empty):
 *     ```
       <?php
       $cat1 = get_post_meta($post->ID, 'level', true);
       if ( $cat1 ) {
        echo '<span class="cat1">';
        echo $cat1;
        echo '</span>';
       }
       ?>
       <?php
       $cat2 = get_post_meta($post->ID, 'time', true);
       if ( $cat2 ) {
        echo '<span class="cat2">';
        echo $cat2;
        echo '</span>';
       }
       ?>
       <?php
       $cat3 = get_post_meta($post->ID, 'cat', true);
       if ( $cat3 ) {
        echo '<span class="cat3">';
        echo $cat3;
        echo '</span>';
       }
       ?>
       ```
   
 * Any ideas for how to make this more efficient?

Viewing 1 replies (of 1 total)

 *  [Kidcompassion](https://wordpress.org/support/users/kidcompassion/)
 * (@kidcompassion)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/combining-multiple-custom-fields-into-single-query/#post-5232412)
 * I would suggest seeing if you can do it with a new WP_Query. You can query multiple
   categories/ custom meta data and then filter your one large query on the page.
   You’ll have to retrieve your custom post data in the $args.
 * Check the custom meta stuff around line 154:
    [http://www.billerickson.net/code/wp_query-arguments/](http://www.billerickson.net/code/wp_query-arguments/)

Viewing 1 replies (of 1 total)

The topic ‘Combining multiple custom fields into single query?’ is closed to new
replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Kidcompassion](https://wordpress.org/support/users/kidcompassion/)
 * Last activity: [11 years, 9 months ago](https://wordpress.org/support/topic/combining-multiple-custom-fields-into-single-query/#post-5232412)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
