My guess is this line:
if ($menu_to_use) :
Because your get_post_meta function will return:
- If only $id is set it will return all meta values in an associative array.
- If $single is set to false, or left blank, the function returns an array containing all values of the specified key.
- If $single is set to true, the function returns the first value of the specified key (not in an array)
If there is nothing to return the function will return an empty array unless $single has been set to true, in which case an empty string is returned.
Since you are getting an empty string, your if statement still holds true because the variable is set and contains something, even if it’s nothing. Make sense 🙂
Basically you want to check if the string is empty. Change that line mentioned above to this:
if( ! empty( $menu_to_use ) ) :
Which states if $menu_to_use is not empty, or contains something other than:
- “” (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- “0” (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- $var; (a variable declared, but without a value)