Support » Plugin: Advanced Custom Fields » [Plugin: Advanced Custom Fields] One value in a none-required fields gets looped out in every post

  • Resolved olofiano


    Hello, this might just be a lack of php skill from my side but i thought id post my problem here since you guys hopefully have some more understanding of my problem.

    I have a post type called Reseller which contains acf for name, phone, mobile, fax, email, adress etc. Some of these fields are not required for input (fax, mobile, etc), and others are. My problem is that when i put a mobilenumber on one reseller that number gets looped out on every reseller… See the code below

    global $post;
    $tmp_post = $post;
    $empty_array = array();
    //setup the query
    $args = array(
    			'post_type' => 'reseller',
    			'meta_key' => 'gw_reseller_name',
    			'orderby' => 'meta_value',
    			'order' => 'ASC',
    			'numberposts' => '-1',
    			'meta_query' => array(
    								'key' => 'gw_reseller_zip',
    								'value' => $thetitle
    // get posts according to $args
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post);
    	echo "<ul class=\"reseller_wrapper\" id=\"reseller-"; the_field('gw_reseller_name'); echo "\">";
    	echo "</ul>";
    	$custom_posts = $empty_array;
    	$post = $tmp_post;

    the function get_reseller() looks like this:

    function get_reseller() {
    // variables that might come in handy later... only for testing
    	global $gw_lev_name_var, $gw_lev_adress_var, $gw_lev_postadress_var, $gw_lev_telnr_var, $gw_lev_fax_var, $gw_lev_mobile_var, $gw_lev_mail_var;
    	$gw_lev_name_var = '<strong><li class="gw_lev_name">' . get_field('gw_lev_name') . '</li></strong> ';
    	$gw_lev_adress_var = '<li class="gw_lev_adress">' . get_field('gw_lev_adress') . '</li> ';
    	$gw_lev_postadress_var = '<li class="gw_lev_postadress">' . get_field('gw_lev_postadress') . '</li> ';
    	$gw_lev_telnr_var = '<li class="gw_lev_telnr"> Tel: ' . get_field('gw_lev_telnr') . '</li> ';
    //These are none-required fields, hence the check if null
    	if(!(get_field('gw_lev_fax') == null)){
    		$gw_lev_fax_var = '<li class="gw_lev_fax">Fax: ' . get_field('gw_lev_fax') . '</li> ';
    	if(!(get_field('gw_lev_mobile') == null))
    		$gw_lev_mobile_var = '<li class="gw_lev_mobile"> Mobil: ' . get_field('gw_lev_mobile') . '</li> ';
    	if(!(get_field('gw_lev_mail') == null))
    		$gw_lev_mail_var = '<li class="gw_lev_mail">E-post: ' . get_field('gw_lev_mail') . '</li> ';
      	echo $gw_lev_name_var, $gw_lev_adress_var, $gw_lev_postadress_var, $gw_lev_telnr_var, $gw_lev_fax_var, $gw_lev_mobile_var, $gw_lev_mail_var; 

    Now, this code is reduntant and in swedish atm but it’s just for making things work before i make it more efficient. Any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Made some prints and realized that if i do

    echo 'Duplicate?';

    the output is

    First reseller
    Mobile: +4670738xx
    Duplicate? +4670738xx

    Second reseller
    Mobile: +4670738xx

    Third reseller
    Mobile: +4670738xx

    and so on. So in fact, the field ‘gw_reseller_mobile’ IS empty for the other posts, but somehow the mobile value from the first post sticks…

    NOTE: nvm that the fieldnames differs in function and in template php. Its just for you guys to understand what the names mean. They are in fact identical…

    Resolved by adding

    $gw_lev_fax_var = '';
    	$gw_lev_mobile_var = '';
    	$gw_lev_mail_var = '';

    before the if statements in the get_reseller() function. Don’t like it though ;P



    FWIW – this is expected behavior because you are declaring those as global variables. Once they are set they will retain their values until they are overwritten, either be a non-null value from the custom field or by the empty string, as in your last post. Remove global… line and it will work without setting them to an empty string (but those variables will also not be available outside the scope of the get_reseller() function)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Advanced Custom Fields] One value in a none-required fields gets looped out in every post’ is closed to new replies.