Title: Creating a PHP Wildcard for Zipcode
Last modified: August 20, 2016

---

# Creating a PHP Wildcard for Zipcode

 *  Resolved [noelgreen](https://wordpress.org/support/users/noelgreen/)
 * (@noelgreen)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/creating-a-php-wildcard-for-zipcode/)
 * I have a form where people enter their zip code in their profile
    I also have
   a custom meta-field so someone can enter a zip code when posting a post.
 * So I can enter “87047” in my profile, and then enter “87112” in a post.
 * I have created a loop on the main page in which I want to display content that
   is relevant to the zip code in which a person has entered. This was part was 
   easy. The problem is I want it to base it on only the first two digits of a person’s
   zip code.
 * So if I enter “87047” as “MY zip code” it only takes the “87” and references 
   posts with “87” in the custom meta-field.
 * I think I need a wildcard array to say, “Are there posts with 87000, 87001, 87002,”
   all the way to “87999” in the custom meta-field and if there are to display entries
   from those posts.
 * Basically if I enter in 87047 I would also “see things” in 87112.
 * Here’s what I have so far…
 *     ```
       <?php
       $zipcode = $data;
       $getlength = strlen($zipcode);
       $thelength = 2;
       $areacode = substr($zipcode, 0, $thelength);
   
       $sqlGroupQuery = "(SELECT * FROM posts WHERE Location LIKE '87%')";
       ?>
   
       <?php echo $areacode ?>
   
       	 <?php
   
       		 $querystr = "
       		    SELECT wposts.*
       		    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
       		    WHERE wposts.ID = wpostmeta.post_id
       		    AND wpostmeta.meta_key = 'Location'
       		    AND wpostmeta.meta_value =  $areacode
       		    AND wposts.post_status = 'publish'
       		    AND wposts.post_type = 'post'
       		    ORDER BY wposts.post_date DESC
       		 ";
   
       	 $pageposts = $wpdb->get_results($querystr, OBJECT);
        	 ?>
       ```
   
 * Like I said, “data” is working, and this is showing “87” when I ‘echo $areacode’
   I just need the “wildcard” part to be added in.

Viewing 1 replies (of 1 total)

 *  Thread Starter [noelgreen](https://wordpress.org/support/users/noelgreen/)
 * (@noelgreen)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/creating-a-php-wildcard-for-zipcode/#post-2133977)
 * With the help of my programming brother we got it!
 * Here’s the solution…
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_

Viewing 1 replies (of 1 total)

The topic ‘Creating a PHP Wildcard for Zipcode’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [truncate](https://wordpress.org/support/topic-tag/truncate/)
 * [zip code](https://wordpress.org/support/topic-tag/zip-code/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [noelgreen](https://wordpress.org/support/users/noelgreen/)
 * Last activity: [14 years, 12 months ago](https://wordpress.org/support/topic/creating-a-php-wildcard-for-zipcode/#post-2133977)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
