• I want to display/echo an HTML table ONLY if user is logged in AND a custom field exists.

    but how do I determine both?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • If your code is in the loop, code like this should work:

    $custom = get_post_meta($post->ID,'my custom field',true);
    if ( is_user_logged_in() && isset($custom) && strlen($custom) ) {
       echo "your html table goes here";
    }

    If not in the Loop, put in the correct post ID.

    Thread Starter Middle8Media

    (@middle8media)

    Thanks.

    I am not sure if this is overkill or bloated code, but I ended up going with this code.

    I search for the current user, check to see if they are logged in, then return their i, then check to see if their custom field exists. if empty, I echo no html, if it does exist, I echo desired html.

    global $current_user;
    get_currentuserinfo();
    $author_id = $current_user->ID;
    $film_custom_field = get_field(‘film_1’, ‘user_’ . $author_id );
    if ( is_user_logged_in() && empty( $film_custom_field ) ) {
    echo ‘ ‘;
    } else {
    echo ‘html goes here’;

    }

    Your code may be perfectly correct, but I cannot vouch for that.

    The get_field() function is part of the Advanced Custom Fields plugin. With the second parameter, the documentation says that it adds the field to the user. I am not certain what that means and what the return value will be.

    I am guessing that this should work (without the need to get the current user info):

    $film_custom_field = get_field('film_1');
    if ( is_user_logged_in() && empty( $film_custom_field ) ) {
       echo ' ';
    } else {
       echo 'html goes here';
    }

    Note that this will fail if the custom field ever contains only a zero or an empty string, because empty() returns ‘true’ for those values.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘if user is logged in AND a custom field exists.’ is closed to new replies.