[resolved] Creating a PHP Wildcard for Zipcode (2 posts)

  1. noelgreen
    Posted 4 years ago #

    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...

    $zipcode = $data;
    $getlength = strlen($zipcode);
    $thelength = 2;
    $areacode = substr($zipcode, 0, $thelength);
    $sqlGroupQuery = "(SELECT * FROM posts WHERE Location LIKE '87%')";
    <?php echo $areacode ?>
    		 $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.

  2. noelgreen
    Posted 4 years ago #

    With the help of my programming brother we got it!

    Here's the solution...

    [Code moderated as per the Forum Rules. Please use the pastebin]

Topic Closed

This topic has been closed to new replies.

About this Topic