Title: Using Custom Select Query and Magic fields
Last modified: August 19, 2016

---

# Using Custom Select Query and Magic fields

 *  Resolved [eian00](https://wordpress.org/support/users/eian00/)
 * (@eian00)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/using-custom-select-query-and-magic-fields/)
 * Hello, i have this perfectly working code showing posts based on two or more 
   custom fields;
 *     ```
       <?php
       global $wpdb;
       global $post;
       $key1 = 'color';
       $val1 = 'red';
       $key2 = 'shape';
       $val2 = 'square';
       $querystr = "
       SELECT wposts.*
       FROM $wpdb->posts wposts, $wpdb->postmeta metacolor, $wpdb->postmeta metawgt
       WHERE wposts.ID = metacolor.post_id
       AND wposts.ID = metawgt.post_id
       AND (metacolor.meta_key = '$key1' AND metacolor.meta_value = '$val1')
       AND (metawgt.meta_key = '$key2' AND metawgt.meta_value = '$val2')
       AND wposts.post_type = 'post'
       AND wposts.post_status = 'publish'
       ORDER BY UPPER(wposts.post_title) ASC
       "; 
   
       $pageposts = $wpdb->get_results($querystr, OBJECT); ?>  <?php if ($pageposts):
       ?>  
   
       <?php global $post; ?>
       <?php foreach ($pageposts as $post): ?>
       <?php setup_postdata($post); ?>
       <div class="post" id="post-<?php the_ID(); ?>">  
   
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">  <?php the_title(); ?></a>
   
       </div>
       <?php endforeach; ?>
       <?php endif; ?>
       ```
   
 * now I installed Magic Fields and it looks like it won’t filter posts with custom
   fields added over Magic fields,
 * is there maybe some small editing needed in the code above?
 * thank you

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [eian00](https://wordpress.org/support/users/eian00/)
 * (@eian00)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/using-custom-select-query-and-magic-fields/#post-1799379)
 * I got it,
 * the problem was that in magic fields for color I used a Checkbox List with the
   list of possible colors, so once you choose the color Red for example, wordpress
   stores it in the database like:
 *     ```
       a:1:{i:0;s:4:"Red";}
       ```
   
 * so the correct form would be:
 *     ```
       <?php
       global $wpdb;
       global $post;
       $key1 = 'color';
       $val1 = 'a:1:{i:0;s:4:"Red";}';
       $key2 = 'shape';
       $val2 = 'square';
       $querystr = "
       SELECT wposts.*
       FROM $wpdb->posts wposts, $wpdb->postmeta metacolor, $wpdb->postmeta metawgt
       WHERE wposts.ID = metacolor.post_id
       AND wposts.ID = metawgt.post_id
       AND (metacolor.meta_key = '$key1' AND metacolor.meta_value = '$val1')
       AND (metawgt.meta_key = '$key2' AND metawgt.meta_value = '$val2')
       AND wposts.post_type = 'post'
       AND wposts.post_status = 'publish'
       ORDER BY UPPER(wposts.post_title) ASC
       "; 
   
       $pageposts = $wpdb->get_results($querystr, OBJECT); ?>  <?php if ($pageposts):
       ?>  
   
       <?php global $post; ?>
       <?php foreach ($pageposts as $post): ?>
       <?php setup_postdata($post); ?>
       <div class="post" id="post-<?php the_ID(); ?>">  
   
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">  <?php the_title(); ?></a>
   
       </div>
       <?php endforeach; ?>
       <?php endif; ?>
       ```
   
 *  [eiwans](https://wordpress.org/support/users/eiwans/)
 * (@eiwans)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/using-custom-select-query-and-magic-fields/#post-1799797)
 * Hi Eian
 * I trying to make query to display list base on one of my magic field checkbox(
   nz or aus)
 * follow is what i have:
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 * Could you help spot me where i did wrong?
 * Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Using Custom Select Query and Magic fields’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [eiwans](https://wordpress.org/support/users/eiwans/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/using-custom-select-query-and-magic-fields/#post-1799797)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
