0

The purpose of the function right now is to iterate through multiple comma separated lists of zip codes (advanced custom field of zip_codes_serviced) and match it to a particular zip code (33606 for testing purposes). If matched the function should print the city and state field associated with that zip code. In this case Tampa, FL. See function below:

function zip_search($userZip){
    $posts = get_posts(array(
        'posts_per_page'    => -1,
        'post_type'         => 'Location'
    ));


    if( $posts ): 
        foreach( $posts as $post ): 
            $zipField=get_field('zip_codes_serviced');
            //echo $zipField;
            $zipString = $zipField . ', ';
            //echo $zipArray;

            $array = explode(', ' , $zipString); //split string into array seperated by ', '

            foreach($array as $value) //loop over values
            {
                $cityField=get_field('city');
                $stateField=get_field('state');

                if($value==$userZip){

                return ($cityField . '<br>' . $stateField);  
            }   
            }       
        endforeach;
        wp_reset_postdata(); 
    endif; 
}

I then call the function like this:

zip_search(33606);

I also tried like this:

function zip_search(){
        $posts = get_posts(array(
            'posts_per_page'    => -1,
            'post_type'         => 'Location'
        ));
        //Set user zip to 33606 for testing purposes
        $userZip=33606;

        if( $posts ): 
            foreach( $posts as $post ): 
                $zipField=get_field('zip_codes_serviced');
                //echo $zipField;
                $zipString = $zipField . ', ';
                //echo $zipArray;

                $array = explode(', ' , $zipString); //split string into array seperated by ', '

                foreach($array as $value) //loop over values
                {
                    $cityField=get_field('city');
                    $stateField=get_field('state');
                    //echo $value. '<br>';            
                    if($value==$userZip){

                    echo ($cityField . '<br>' . $stateField); //print 
                }   
                }       
            endforeach;
            wp_reset_postdata(); 
        endif; 
    }

Then called like this:

zip_search();

Shouldn't this print out the city and state field on the page? When I put the logic outside of a function it works but I can't get it to work within a function. Does anyone have any advice?

The only way I could get it to work is completely outside a function-- creating a file zip-search.php and then inserting <?php include("phone-display.php"); ?> on the page where I want it to display, I am pretty sure this is a hack though; zip-search.php seen below :

$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'Location'
));
//Set user zip to 33606 for testing purposes
$userZip=33606;
if( $posts ): 
     foreach( $posts as $post ): 
        $zipField=get_field('zip_codes_serviced');
        //echo $zipField;
        $zipString = $zipField . ', ';
        //echo $zipArray;

        $array = explode(', ' , $zipString); //split string into array seperated by ', '

        foreach($array as $value) //loop over values
        {
            $cityField=get_field('city');
            $stateField=get_field('state');
            //echo $value. '<br>';            
            if($value==$userZip){

               echo ($cityField . '<br>' . $stateField); //print 
           }    
         }       
     endforeach;
    wp_reset_postdata(); 
 endif; 

The entire template file I am trying to get the zip code function to work in is seen below:

<?php 
 /*
Template Name: Location
*
*/
get_header(); 
nectar_page_header($post->ID); 

//full page
$fp_options = nectar_get_full_page_options();
extract($fp_options);

?>
<?php
function zip_search($userZip){
    $posts = get_posts(array(
        'posts_per_page'    => -1,
        'post_type'         => 'Location'
    ));
    //Set user zip to 33606 for testing purposes
    //$userZip=33606;

    if( $posts ): 
        foreach( $posts as $post ): 
            $zipField=get_field('zip_codes_serviced');
            //echo $zipField;
            $zipString = $zipField . ', ';
            //echo $zipArray;

            $array = explode(', ' , $zipString); //split string into array seperated by ', '

            foreach($array as $value) //loop over values
            {
                $cityField=get_field('city');
                $stateField=get_field('state');
                //echo $value. '<br>';            
                if($value==$userZip){

                echo ($cityField . '<br>' . $stateField); //print 
            }   
            }       
        endforeach;
        wp_reset_postdata(); 
    endif; 
}
?>


<div class="container-wrap">

    <div class="<?php if($page_full_screen_rows != 'on') echo 'container'; ?> main-content">

        <div class="row">

            <?php 

            //breadcrumbs
            if ( function_exists( 'yoast_breadcrumb' ) && !is_home() && !is_front_page() ){ yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } 

             //buddypress
             global $bp; 
             if($bp && !bp_is_blog_page()) echo '<h1>' . get_the_title() . '</h1>';

             //fullscreen rows
             if($page_full_screen_rows == 'on') echo '<div id="nectar_fullscreen_rows" data-animation="'.$page_full_screen_rows_animation.'" data-row-bg-animation="'.$page_full_screen_rows_bg_img_animation.'" data-animation-speed="'.$page_full_screen_rows_animation_speed.'" data-content-overflow="'.$page_full_screen_rows_content_overflow.'" data-mobile-disable="'.$page_full_screen_rows_mobile_disable.'" data-dot-navigation="'.$page_full_screen_rows_dot_navigation.'" data-footer="'.$page_full_screen_rows_footer.'" data-anchors="'.$page_full_screen_rows_anchors.'">';

                 if(have_posts()) : while(have_posts()) : the_post(); 

                     the_content(); 

                 endwhile; endif; 

            if($page_full_screen_rows == 'on') echo '</div>'; ?>

        </div><!--/row-->

    </div><!--/container-->
    <span><?php zip_search(33606);?></span>
    <span>Locations Landing Page--where zip code function can go </span>
</div><!--/container-wrap-->

<?php get_footer(); ?>

See the results of the var_dump($post); within function below, I only included a small portion because of character limits:

array(37) { [0]=> object(WP_Post)#1683 (24) { ["ID"]=> int(1490) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:39:29" ["post_date_gmt"]=> string(19) "2018-09-21 15:39:29" ["post_content"]=> string(0) "" ["post_title"]=> string(14) "Scottsdale, AZ" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(13) "scottsdale-az" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 20:54:47" ["post_modified_gmt"]=> string(19) "2018-09-21 20:54:47" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1490" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [1]=> object(WP_Post)#1684 (24) { ["ID"]=> int(1488) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:37:36" ["post_date_gmt"]=> string(19) "2018-09-21 15:37:36" ["post_content"]=> string(0) "" ["post_title"]=> string(17) "Missouri City, TX" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(16) "missouri-city-tx" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 15:37:36" ["post_modified_gmt"]=> string(19) "2018-09-21 15:37:36" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1488" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [2]=> object(WP_Post)#1685 (24) { ["ID"]=> int(1486) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:35:35" ["post_date_gmt"]=> string(19) "2018-09-21 15:35:35" ["post_content"]=> string(0) "" ["post_title"]=> string(11) "Houston, TX" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(10) "houston-tx" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 15:35:35" ["post_modified_gmt"]=> string(19) "2018-09-21 15:35:35" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1486" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [3]=> object(WP_Post)#1686 (24) { ["ID"]=> int(1484) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:29:24" ["post_date_gmt"]=> string(19) "2018-09-21 15:29:24" ["post_content"]=> string(0) "" ["post_title"]=> string(9) "Sugarland" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(9) "sugarland" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-10-10 19:29:31" ["post_modified_gmt"]=> string(19) "2018-10-10 19:29:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1484" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [4]=> object(WP_Post)#1687 (24) { ["ID"]=> int(1482) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:27:46" ["post_date_gmt"]=> string(19) "2018-09-21 15:27:46" ["post_content"]=> string(0) "" ["post_title"]=> string(13) "Grapevine, TX" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(12) "grapevine-tx" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 15:27:46" ["post_modified_gmt"]=> string(19) "2018-09-21 15:27:46" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1482" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [5]=> object(WP_Post)#1688 (24) { ["ID"]=> int(1480) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:25:31" ["post_date_gmt"]=> string(19) "2018-09-21 15:25:31" ["post_content"]=> string(0) "" ["post_title"]=> string(13) "Southlake, TX" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(12) "southlake-tx" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 15:25:31" ["post_modified_gmt"]=> string(19) "2018-09-21 15:25:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1480"

Blaise
  • 137
  • 11
  • 1
    could you var_dump() `$post` variable inside the function after retrieving?, show us result –  Oct 22 '18 at 15:41
  • 2
    where do you use return? – treyBake Oct 22 '18 at 15:47
  • 1
    also var dump what you echo - they may be empty strings - foreach not being hit etc. and ulitmately nothing gets echoed out – treyBake Oct 22 '18 at 15:47
  • @AnimeshSahu see var_dump() of $post above – Blaise Oct 22 '18 at 16:10
  • @ThisGuyHasTwoThumbs Ok yea I am getting a bunch of null values for the when I var_dump($cityField). For some reason $cityField=get_field('city'); is not getting the city field. Thanks for the suggestion-- I'm pretty new to this so I didn't even know about var_dump()-- very helpful. – Blaise Oct 22 '18 at 16:29
  • So I am getting NULL values for basically any of the variables I try to get from custom fields. Although I get the post information and I doesn't behave like this if I use this logic outside a function. – Blaise Oct 22 '18 at 17:55
  • 1
    funny, i don't see zip_codes_serviced field in the return data ($post). Should it be there? – norrin Oct 22 '18 at 22:02
  • @Anthony Yes it should be, so it looks like none of the custom fields are not showing up. – Blaise Oct 23 '18 at 13:55
  • 1
    Are you sure you are using `get_field` function correctly? I assume that using it in a way you do you should add post id as parameter. – Valdars Oct 23 '18 at 14:28
  • 1
    I am not actually familiar with that function so much but usually id is second parameter. – Valdars Oct 23 '18 at 15:05
  • @Valdars Yes it is. I just hard-coded a post number into the parameter and it worked. Now I just have to find how to access the post number and assign to a variable each loop iteration. – Blaise Oct 23 '18 at 15:11
  • 1
    I haven't touched wordpress in years but from what I vaguely remember use get_metadata function, here's the link: https://codex.wordpress.org/Function_Reference/get_metadata Conversely, you can use hooks when adding a new post and use add_metadata: https://codex.wordpress.org/Function_Reference/add_metadata – norrin Oct 23 '18 at 17:58
  • @Anthony So I took a look at that doc but it looks like I have to specify the post ID with the `get_metadata` function. I tried keeping it null but it wouldn't return anything. Is there a function that can just return the ID of each `$post` or a way to index into each `$post` to grab the ID. From `var_dump($post)` in my loop I see the ID is there. I'm just not sure how to access with that array. – Blaise Oct 23 '18 at 18:43
  • @Anthony You were right. I was just misunderstanding the docs. Appreciate the tip. Now I have access to the info I need. – Blaise Oct 23 '18 at 19:16
  • So now the problem I am having is that this post meta data is wrong. It looks like it is from the original location post type I made. Also still not sure how I can get the `object(WP_Post)#` from this which is what I need for the `get_field()` function. – Blaise Oct 23 '18 at 20:28
  • @DanielRogers the solution might be a combination of using an edit_post hook and calling update_metadata function inside it. This is to correct/update that wrong post metadata, I hope this helps. – norrin Oct 24 '18 at 03:47

1 Answers1

1

I ended up doing this and in the end decided it would be better to return the permalink for the post rather that the city and state:

function zip_search($userZip){

    $args = array(
    'posts_per_page'    => -1,
    'post_type'         => 'Locations'
    );

$wp_query = new WP_Query($args); 

if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post();
      $zipField=get_field('zip_codes_services');

          $zipString = $zipField . ', ';        

          $array = explode(', ' , $zipString); //split string into array seperated by ', '

        foreach($array as $value) //loop over values
        {

            if($value==$userZip){
                $post_id = get_the_ID();
                $permalink=get_permalink($post_id);                 
               return ($permalink); //print 
           }    

        }
       endwhile; 
       wp_reset_postdata(); 
endif;
}
Blaise
  • 137
  • 11