Support

Account

Home Forums Front-end Issues get_fields always returning true

Solving

get_fields always returning true

  • Hi there,

    I’m trying to display a full block of fields on the basis of there being any fields populated. I’m surrounding them with a div for styling.

    When I run the following code:

    $fields = get_fields();
    	var_dump($fields);
    	if($fields){
    		echo "True";
    	} else {
    		echo "False";
    	}
    if($fields) { ?>
    	<div class="contact d">
    		<h3>Contact Information</h3>
    		<div class="contact-info">
    			<?php if (get_field('name')) { ?>
    			...
    			etc

    It always displays “True” (and subsequently, the surrounding Contact Info div) even though no fields have been populated. Am I missing something, is this not the way it’s supposed to work? I can see from the var_dump that it’s an object, but all of the field values are => string(0)

    Appreciate any feedback, this is a fantastic plugin, I love using it. Convinced I had this working before. Maybe I’m missing something.

    Cheers,

    T

  • Hi @tadywankenobi

    The get_fields function should not be returning an object. Can you please provide the var_dump output?

    Thanks
    E

  • Hi Elliot,

    Thanks for taking the time to look at this for me. The output of the var_dump is:

    array(8) { ["title"]=> string(0) "" ["name"]=> string(0) "" ["address"]=> string(0) "" ["email"]=> string(0) "" ["website"]=> string(0) "" ["telephone"]=> string(0) "" ["telephone2"]=> string(0) "" ["telephone3"]=> string(0) "" }

    (Sorry, should have included it with the original question)

    It’s weird. I’m fully convinced I have this functionality working on another project, I just can’t remember which one.

    Cheers,

    T

  • Hi @tadywankenobi

    What is happening here, is you have saved the post with no values. The get_fields function will return all the fields on the page with their values. In this case, all the values are empty.

    Perhaps you need to modify your code to better handle the data returned.

    Thanks
    E

  • But isn’t that what get_fields is supposed to do? Is there away I can use get_fields in this manner, to see if any of the content has been populated?

  • Hi @tadywankenobi,

    You could check to see if a required field is empty in the first post?
    Eg if get_field(‘name’) is empty then do something, else something else

    cheers

  • Hi @tadywankenobi

    The get_fields function will return all field data, even if that field data is empty.

    You can check all the values like so:

    
    <?php 
    
    $fields = get_fields();
    $empty = true;
    
    if( !empty($fields) )
    {
    	foreach( $fields as $name => $value )
    	{
    		if( !empty($value) )
    		{
    			$empty = false;
    		}
    	}
    }
    
    echo '<pre>';
    	var_dump( $empty );
    echo '</pre>';
    die;
    
     ?>
    

    Thanks
    E

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘get_fields always returning true’ is closed to new replies.