0

I have a custom field in my wordpress user profile : instrument

I have two users with the role subscriber. One hav the instrument Altosaxofon and one Tenorsaxofon.

With this code I want to display "Tenorsaxofon", but I get "TenorsaxofonTenorsaxofon"

<?php
$args1 = array('role' => 'subscriber');
$subscribers = get_users($args1);
foreach ($subscribers as $user) {
    if ( $user->instrument = 'Tenorsaxofon' )
    {
        echo $user->instrument ;
    }
}
?>

Why is that?

Best regards, Kresten

Kresten
  • 810
  • 13
  • 36

2 Answers2

2

My guess is that you're using a single equals (=) instead of a double equals (==) to check the person's instrument. A single equals (=) signifies an assignment, and it returns true if the assignment worked. I think you want to use a double equals (==) to instead check if $user->instrument is the same as Tenorsaxofon.

If this is the case, you'll want to change this:

if ( $user->instrument = 'Tenorsaxofon' )

To this:

if ( $user->instrument == 'Tenorsaxofon' )

(More about that here).


It also could be that $subscribers contains two users who have their instrument set to Tenorsaxofon. If there's two subscribers with the same instrument, then your echo will print Tenorsaxofon twice, resulting in TenorsaxofonTenorsaxofon

Ethan
  • 4,295
  • 4
  • 25
  • 44
1

try this, your missing a = sign $user->instrument == 'Tenorsaxofon'

<?php    
$args1 = array('role' => 'subscriber');
$subscribers = get_users($args1);
foreach ($subscribers as $user) {
    if ( $user->instrument == 'Tenorsaxofon' )
    {
        echo $user->instrument ;
    }
}
Tony
  • 859
  • 1
  • 7
  • 19