0

I'm trying to connect some user_meta data from registration form to some categories set up in admin to filter the presentation on the page. This one is tripping me up because the question has multiple answers ...

Here is what I have set up so far ...

$user_id = $current_user->ID;

$user_values = get_user_meta( $user_id);

$interest_disaster = $user_values['interest_disaster'][0];
$interest_animals = $user_values['interest_animals'][0];
$interest_women = $user_values['interest_women'][0];
$interest_seniors = $user_values['interest_seniors'][0];
$interest_hunger_health = $user_values['interest_hunger_health'][0];
$interest_education = $user_values['interest_education'][0];
$interest_environment = $user_values['interest_environment'][0];
$interest_arts_culture = $user_values['interest_arts_culture'][0];
$interest_children_youth = $user_values['interest_children_youth'][0];
$interest_other = $user_values['interest_other'][0];
$interest_mentoring = $user_values['interest_mentoring'][0];
$interest_poverty_urban = $user_values['interest_poverty_urban'][0];

I can stick those in an array ...

$interestBlock = array(
    $interest_disaster,
    $interest_animals,
    $interest_women,
    $interest_seniors,
    $interest_hunger_health,
    $interest_education,
    $interest_environment,
    $interest_arts_culture,
    $interest_children_youth,
    $interest_other,
    $interest_mentoring,
    $interest_poverty_urban
    );

But I am failing badly at checking if the item exists in my category array inside a function and echoing values set in $user_values array out to page or sticking them in a custom query.

function convertInterestCategory(){

    $interestCategories = array(
    'interest_disaster',
    'interest_animals',
    'interest_women',
    'interest_seniors',
    'interest_hunger_health',
    'interest_education',
    'interest_environment',
    'interest_arts_culture',
    'interest_children_youth',
    'interest_other',
    'interest_mentoring',
    'interest_poverty_urban'
    );
$counter = 0;

//Stuff in usermeta

    foreach ($interestCategories as $counter) {
        if (in_array($interestCategories , $user_values)){
        echo $user_values[$interestCategories][0];
        $counter++;
        }
    }
}

I'd appreciate help in putting together a loop function to check if item exists in $user_values array and is also in $interestCategories array, then how to return relevant results. Thanks in advance.

One by one it would look like this I guess ...

//Interests

if (isset($user_values['interest_disaster'][0])){
    $interest_disaster = $user_values['interest_disaster'][0];
    echo $interest_disaster;    
}

if (isset($user_values['interest_animals'][0])){
    $interest_animals = $user_values['interest_animals'][0];
    echo $interest_animals; 
}

if (isset($user_values['interest_women'][0])){
    $interest_women = $user_values['interest_women'][0];
    echo $interest_women;
}

if (isset($user_values['interest_seniors'][0])){
    $interest_seniors = $user_values['interest_seniors'][0];
    echo $interest_seniors;
}

if (isset($user_values['interest_hunger_health'][0])){
    $interest_hunger_health = $user_values['interest_hunger_health'][0];
    echo $interest_hunger_health;
}

if (isset($user_values['interest_education'][0])){
    $interest_education = $user_values['interest_education'][0];
    echo $interest_education;
}

if (isset($user_values['interest_environment'][0])){
    $interest_environment = $user_values['interest_environment'][0];
    echo $interest_environment;
}

if (isset($user_values['interest_arts_culture'][0])){
    $interest_arts_culture = $user_values['interest_arts_culture'][0];
    echo $interest_arts_culture;
}

if (isset($user_values['interest_children_youth'][0])){
    $interest_children_youth = $user_values['interest_children_youth'][0];
    echo $interest_children_youth;
}

if (isset($user_values['interest_other'][0])){
    $interest_other = $user_values['interest_other'][0];
    echo $interest_other;
}

if (isset($user_values['interest_mentoring'][0])){
    $interest_mentoring = $user_values['interest_mentoring'][0];
    echo $interest_mentoring;
}

if (isset($user_values['interest_poverty_urban'][0])){
    $interest_poverty_urban = $user_values['interest_poverty_urban'][0];
    echo $interest_poverty_urban;
}

1 Answer 1

0

Are you looking for this?

function convertInterestCategory() {

    $interest_categories = array(
        'interest_disaster',
        'interest_animals',
        'interest_women',
        'interest_seniors',
        'interest_hunger_health',
        'interest_education',
        'interest_environment',
        'interest_arts_culture',
        'interest_children_youth',
        'interest_other',
        'interest_mentoring',
        'interest_poverty_urban'
    );

    $current_user = wp_get_current_user();
    $user_values  = get_user_meta( $current_user->ID );

    foreach ( $interest_categories as $interest_category ) {
        if ( isset( $user_values[$interest_category][0] ) )
            echo $user_values[$interest_category][0];
    }
}
3
  • Nice, looks good to me, but it doesn't work and I get no errors out of wp_debug. Commented May 12, 2013 at 22:33
  • @Chrisdigital Check the edit. Commented May 13, 2013 at 1:39
  • Your update works! with a tiny bug caught by wp_debug $user_values = get_user_meta( $current_user->ID; ); should be $user_values = get_user_meta( $current_user->ID ); no semi colon after ID. Edit your entry and I will accept the answer. Thank you very much! Commented May 13, 2013 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.