Community Support Forums — WordPress® ( Users Helping Users ) — 2011-03-28T23:25:02-05:00 http://www.primothemes.com/forums/feed.php?f=4&t=627 2011-03-28T23:25:02-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=8539#p8539 <![CDATA[Re: Display custom fields in posts.]]>
I'm pretty amazed that there isn't an easier way to do this yet, but I think what I've come up with is reasonably straight forward.

My first discovery was that I could in fact get to the data simply using get_userdata(). Oddly, as far as I can tell (i'm running WP3.1 and s2member 3.5.6), the data is not stored in an json array but rather just a regular array.

To get a custom s2member field for a specific user:

Code:
$person = get_userdata( ID );
echo $person->wp_s2member_custom_fields["custom_field_key"]; 

"ID" is a user's ID and "custom_field_key" is whatever field you want to retrieve.

Next, I combined this with the new (as of WP 3.1) get_users() function:

Code:
$nwfa_members = get_users();
foreach ($nwfa_members as $user) {
   $s2_data = get_userdata($user->ID);
   echo $user->display_name . ' - City: ' . $s2_data->wp_s2member_custom_fields["s2_city"];
}
 

In this case, I displayed "[USERNAME] - City: [CUSTOM_CITY_FIELD]".

I couldn't find anyway for get_users() to get the wp_s2member_custom_fields directly, hence the intermediate step of grabbing the user ID and then using get_userdata to get the s2 custom fields.

I am NOT a PHP programmer so it's possible that there are wildly better ways to do this. However, this seems like a step in the right direction, if nothing else.

Please give me feedback if you have any. I'm using this to put together a member directory on my site.

Statistics: Posted by mrwweb — March 28th, 2011, 11:25 pm


]]>
2010-11-10T03:00:01-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=4594#p4594 <![CDATA[Re: Display custom fields in posts.]]>
Using the above method as well as a few different methods using OOP it appears there is no easy way to get more then the first record's data, period.

Now, any other field not encoded as the custom fields are can be retrieved in multiples, but that really doesn't solve my, or anyone's problems.

What goes wrong isn't retrieving all of the data, that I can do. As soon as a single custom field is unserialized it prevents further fields from being unserialized.

It's terrible to say, but these forums don't really come with guidance or answers so it looks like I'll be turning to Joomla for my CMS and AEC or some similar plug-in to achieve my needs.

Statistics: Posted by Guest — November 10th, 2010, 3:00 am


]]>
2010-11-09T02:14:05-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=4578#p4578 <![CDATA[Re: Display custom fields in posts.]]>
Code:
//////////////////////////////
//     Match criteria       //
//////////////////////////////
$field_name = "samplefield1";     
$Match_1 = "somethign to write here";                           
$Match_2 = "";                           
//////////////////////////////
$user_query = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users " ));
$UserData = count($user_query); //UserID;  to be set as a counter on a later loop edit

for($t=1;$t<=$UserData;$t++){
//create the query to the database
$query = "SELECT meta_value FROM wp_usermeta WHERE user_id='".$t."' && meta_key='wp_s2member_custom_fields'";
$results = $wpdb->get_col($query); //set the results to a usable variable
/*
## create an array to holdand unserialize the string,
## which places it into a key=>value pair with 'key' being
## the field name and 'value' being the value of the field
*/
$field_value_pairs = unserialize ($results[0]);
/*
# create a multidimensional array to connect all of
# the field=>value pairs to the user
*/
if(!empty($field_value_pairs)){
  //if($field_value_pairs[$field_name] = Match_1)
  if(in_array($Match_1,$field_value_pairs, TRUE))
  {
    //if match is found, add it to the array
   $Fields_By_User[$t] = $field_value_pairs;
  }
}

}//end for loop
/*
# To access the array you need to use the the keys of the
# user [$UserData] and the field names ['samplefield1']
# so if you had a field with the name 'city' you would get
# it's value buy typing $Fields_By_User[$UserData]['city'];
*/
//echo $Fields_By_User[$UserData]['samplefield1'];
echo "<br/>";
print_r($Fields_By_User);
echo "<br/>";

Statistics: Posted by Guest — November 9th, 2010, 2:14 am


]]>
2010-11-08T15:12:17-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=4568#p4568 <![CDATA[Re: Display custom fields in posts.]]>
Code:
//////////////////////////////
//     Match criteria       //
//////////////////////////////
$field_name = "samplefield1";     
$Match_1 = "";                           
$Match_2 = "";                           
//////////////////////////////
$user_query = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users " ));
$UserData = count($user_query); //UserID;  to be set as a counter on a later loop edit

for($t=0;$t<$UserData;$t++){
//create the query to the database
$query = "SELECT meta_value FROM wp_usermeta WHERE user_id='$t' && meta_key='wp_s2member_custom_fields'";

$results = $wpdb->get_col($query); //set the results to a usable variable
/*
## create an array to holdand unserialize the string,
## which places it into a key=>value pair with 'key' being
## the field name and 'value' being the value of the field
*/
$field_value_pairs = unserialize ($results[0]);
/*
# create a multidimensional array to connect all of
# the field=>value pairs to the user
*/

  if($field_value_pairs[$field_name] == Match_1)
  {
    //if match is found, add it to the array
   $Fields_By_User[$t] = $field_value_pairs;
  }
/* sample to match 2 fields 
if($field_value_pairs[$field_name] == Match_1 && $field_value_pairs[$field_name] == Match_2)
{$Fields_By_User[$i] = $field_value_pairs;}
*/
}//end for loop
/*
# To access the array you need to use the the keys of the
# user [$UserData] and the field names ['samplefield1']
# so if you had a field with the name 'city' you would get
# it's value buy typing $Fields_By_User[$UserData]['city'];
*/
//echo $Fields_By_User[$UserData]['samplefield1'];
echo "<br/>";
print_r($Fields_By_User);


So what's really going on here?

We set the search criteria we want be typing the field name and field value we want to match.

Then we count all the users in the database and use the total as a counter

Next we use the counter to iterate through all the users and determine if any of the fields match.

If the fields match, we add the user and fields to an array we can use later

Pretty straight forward so far...

Once I get it all working efficiently and properly I can create a function and add it to the administration panel of s2Members as well as a sub-function allowing easy addition into posts/pages of results.

Right now it will search by custom field, but I'll likely think about adding a radio button in the administration to choose 'custom fields' or 'WP_Login' to search by. More searches could be added, but I'm not there yet.

Statistics: Posted by Guest — November 8th, 2010, 3:12 pm


]]>
2010-11-08T00:35:13-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=4555#p4555 <![CDATA[Re: Display custom fields in posts.]]>
Code:
$UserData = 3;//UserID;  to be set as a counter on a later loop edit

//create the query to the database
$query = "SELECT meta_value FROM wp_usermeta WHERE user_id='$UserData' && meta_key='wp_s2member_custom_fields'";

$results = $wpdb->get_col($query); //set the results to a usable variable
/*
## create an array to holdand unserialize the string,
## which places it into a key=>value pair with 'key' being
## the field name and 'value' being the value of the field
*/
$field_value_pairs = unserialize ($results[0]);

/*
# create a multidimensional array to connect all of
# the field=>value pairs to the user
*/
$Fields_By_User = array
(
$UserData => $field_value_pairs
);
/*
# To access the array you need to use the the keys of the
# user [$UserData] and the field names ['samplefield1']
# so if you had a field with the name 'city' you would get
# it's value buy typing $Fields_By_User[$UserData]['city'];
*/
echo $Fields_By_User[$UserData]['samplefield1'];


An un-commented example of how you would create a multidimensional array of users with field values would be: **Note, the loop is not tested and is for examples of use only.

Code:
$user_query = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users " ));
$UserData = count($user_query);

   $Fields_By_User = array(
   
for($i=0;$i<$UserData;$i++)
{
   $query = "SELECT meta_value FROM wp_usermeta WHERE user_id='$i' && meta_key='wp_s2member_custom_fields'";
   $results = $wpdb->get_col($query);
   $field_value_pairs = unserialize ($results[0]);

      $i => $field_value_pairs
   
}  );


Not bad for 10 lines of code, eh? Anyone able to help me make it even better?

Statistics: Posted by Guest — November 8th, 2010, 12:35 am


]]>
2010-11-07T19:38:31-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=4552#p4552 <![CDATA[Re: Display custom fields in posts.]]>
Someone could likely create a far better array loop and, if so, please do and post

Code:
$UserData = 3;//UserID;  to be set as a counter on a later loop edit

//getting the info form the system

//create the query to the database
$query = "SELECT meta_value FROM wp_usermeta WHERE user_id='$UserData' && meta_key='wp_s2member_custom_fields'";
/*
## deprecated in favor of default wordpress query
##
## $resultx = @mysql_query ($query);
## $rowx = mysql_fetch_array ($resultx, MYSQL_NUM);
## if(mysql_num_rows($resultx) != 0) {
## $not = $rowx[0];
*/
$resultx = $wpdb->get_col($query); //set the results to a usable variable
/*
##  Seperate the variable into an array of strings
##  using the quotation mark " as the seperator
*/
$custom_field_array = explode('"', $resultx[0]);
/*
## to avoid the data we don't want we need to skip every second number
## array [1] is the field name, array [3] is the value
## using the 1 and 3 logic, the next field/value pair will be [5] and [7],
## then [9] and [11] etc
*/
$FieldData[$custom_field_array[1]] = $custom_field_array[3]; //set first array field and value
$Remaining_array_count = count($custom_field_array) -4; //offset count to compensate for first value set
//iterate through the array to retrieve the remaining key/value pairs and insert them to the new array
for($i=3;$i<$Remaining_array_count;$i){
$FieldData[$custom_field_array[$i+2]] = $custom_field_array[$i+4];
$i = $i+2;
}
//set the resulting array into a multidimensional array using UserData
//to 'index' custom fields and contain them per user
$Fields_By_User = array
(
$UserData => $FieldData
);
//print the array to test results
print_r($Fields_By_User);

//print the count being iterated on original array
echo "<br/>".$Remaining_array_count;

Statistics: Posted by Guest — November 7th, 2010, 7:38 pm


]]>
2010-11-07T04:18:56-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=4538#p4538 <![CDATA[Re: Display custom fields in posts.]]>
Code:
//getting the info form the system

//create the query to the database
$query = "SELECT meta_value FROM wp_usermeta WHERE user_id='3' && meta_key='wp_s2member_custom_fields'";
/*
## deprecated in favor of default wordpress query
##
## $resultx = @mysql_query ($query);
## $rowx = mysql_fetch_array ($resultx, MYSQL_NUM);
## if(mysql_num_rows($resultx) != 0) {
## $not = $rowx[0];
*/
$resultx = $wpdb->get_col($query); //set the results to a usable variable
/*
##  Seperate the variable into an array of strings
##  using the quotation mark " as the seperator
*/
$custom_field_array = explode('"', $resultx[0]);
/*
## to avoid the data we don't want we need to skip every second number
## array [1] is the field name, array [3] is the value
## using the 1 and 3 logic, the next field/value pair will be [5] and [7],
## then [9] and [11] etc
*/
echo "field name: " . $custom_field_array[5] . "<br/>";
echo "field value: " . $custom_field_array[7];

Statistics: Posted by Guest — November 7th, 2010, 4:18 am


]]>
2010-11-07T04:18:44-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=4537#p4537 <![CDATA[Re: Display custom fields in posts.]]>
Code:
//getting the info form the system
$query = "SELECT meta_value FROM wp_usermeta WHERE user_id='3' && meta_key='wp_s2member_custom_fields'";
$resultx = @mysql_query ($query);
$rowx = mysql_fetch_array ($resultx, MYSQL_NUM);
if(mysql_num_rows($resultx) != 0) {
$not = $rowx[0];

$custom_field_array = explode('"', $not);

// array [1] is the field name, array [3] is the value
echo "field name: " . $custom_field_array[1] . "<br/>";
echo "field value: " . $custom_field_array[3];
}


What I need it for is to be able to search all of my users based on custom field names and values for every user. Such as a custom field named 'city'.

For me to be able to do this I need to loop through all the users by count (like this)
Code:
$query = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users " ));
$totalrows = count($query);

while grabbing the above code and placing it all in an array that I can then create a search function for that will locate all users that live in, say, Seattle.

What would be nice is to have a built in search feature for the plug-in that would allow for easy searching of any field, including the custom fields. My current modification would not be very efficient. Fortuanately I have a couple more days to work the concept before applying it to a live project.

to do:

    find a way to modify query using $wpdb->get_results
    create efficient loop
    create effective array to hold user data
    create search function for array

I'd love for Jason Caldwell to help me streamline this, or create a usable function for everyone!

Statistics: Posted by Guest — November 7th, 2010, 4:18 am


]]>
2010-09-21T19:21:51-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=3387#p3387 <![CDATA[Re: Display custom fields in posts.]]> Statistics: Posted by MikeL — September 21st, 2010, 7:21 pm


]]>
2010-09-21T10:55:19-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=3373#p3373 <![CDATA[Re: Display custom fields in posts.]]> I hack in a script of my own
It works by the ID of the user,
then it pulls the info from the user meta table,
break the custom fields in to a array with the key being the name on your custom field.
the last thing is the table names on the default

I hope it help you out

<?php
$query = "SELECT ID, user_login FROM wp_users ORDER BY ID ASC";
$result = @mysql_query ($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
if($row['user_login'] != 'admin'){
$id = $row['ID'];
//getting the info form the system
$query = "SELECT meta_value FROM wp_usermeta WHERE user_id='$id' && meta_key='wp_s2member_custom_fields'";
$resultx = @mysql_query ($query);
$rowx = mysql_fetch_array ($resultx, MYSQL_NUM);
if(mysql_num_rows($resultx) != 0) {
$not = $rowx[0];
$str = str_replace("s:", "/", $not);
$str = str_replace("a:", "", $str);
$str = str_replace("{", "", $str);
$str = str_replace("}", "", $str);
$array_sucio = explode("/", $str);
$array_limpio = array();
$i = 0;
foreach ($array_sucio as $c) {
$array_limpio[$i] = $c;
$i++;
}
$done = count($array_limpio);
$a = 0;
for ($i = 1; ; $i++) {
if($done == $i){
break;
}
$start = stripos ( $array_limpio[$i] , ':');
$rest = substr($array_limpio[$i], 0, $start);
$start+= 2;
$finsh[$a] = substr($array_limpio[$i], $start, $rest);
$a++;
if ($i == 20) {break;}
}
$a=0;
$done = count($finsh);
$done = $done / 2;
for ($i = 1; ; $i++) {
$start = $finsh[$a];
$a++;
$end = $finsh[$a];
$fd[$start] = $end;
$a++;
if($done == $i){
break;
}
if ($i == 40) {break;}
}
?>

Statistics: Posted by Guest — September 21st, 2010, 10:55 am


]]>
2010-08-30T19:16:48-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=2759#p2759 <![CDATA[Re: Display custom fields in posts.]]> ~ Much appreciated.

I'll see what we can do.

Statistics: Posted by Jason Caldwell — August 30th, 2010, 7:16 pm


]]>
2010-08-30T19:13:28-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=2758#p2758 <![CDATA[Re: Display custom fields in posts.]]>
Well, thanks for your help but lets hope this gets put up near the top of the todo list.

Statistics: Posted by MikeL — August 30th, 2010, 7:13 pm


]]>
2010-08-30T18:40:31-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=2757#p2757 <![CDATA[Re: Display custom fields in posts.]]>
I see what you mean.
s2Member does NOT provide this feature just yet.
It's on our @TODO list though.
viewtopic.php?f=4&t=618&p=2687&hilit=members+list#p2687

Until then, you might want to have a look at this plugin:
http://wordpress.org/extend/plugins/members-list/

Statistics: Posted by Jason Caldwell — August 30th, 2010, 6:40 pm


]]>
2010-08-30T18:36:24-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=2756#p2756 <![CDATA[Re: Display custom fields in posts.]]> Statistics: Posted by MikeL — August 30th, 2010, 6:36 pm


]]>
2010-08-30T09:50:36-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=2691#p2691 <![CDATA[Re: Display custom fields in posts.]]>
Yes. Take a look inside your Dashboard, under:
s2Member -> API Scripting

Be sure to upgrade to the latest release,
because s2Member v3.2.3+ includes support for Shortcodes as well.

Taken from the API Constants sub-section:

S2MEMBER_CURRENT_USER_FIELDS
This will always be a JSON encoded array, in (string) format. An empty JSON encoded array, in (string) format, if not logged in. This JSON encoded array will contain the following fields: id, ip, email, login, first_name, last_name, display_name, subscr_id, custom. If you've configured additional Custom Fields, those Custom Fields will also be added to this array. For example, if you configured the Custom Field: Street Address, it would be included in this array as: street_address. Custom Field references are converted to lowercase format, and spaces are replaced by underscores.

Code:
<?php $fields = json_decode(S2MEMBER_CURRENT_USER_FIELDS, true); ?>
<?php echo $fields["first_name"]; ?> <?php echo $fields["last_name"]; ?>


Custom Fields are also included in the JSON decoded array.
( Displays a full list of all associative array elements. )
Code:
<?php print_r(json_decode(S2MEMBER_CURRENT_USER_FIELDS, true)); ?>


s2member Shortcode Equivalents
( NEW ~ much easier )


[s2Get user_field="first_name" /]
[s2Get user_field="last_name" /]
[s2Get user_field="Website URL" /]
[s2Get user_field="My Custom Field Name" /]
[s2Get user_field="my_custom_field_name" /]
[s2Get user_field="any other WP_User property" /]

You can also pull details from the meta table.
[s2Get user_option="s2member_custom" /]
[s2Get user_option="s2member_subscr_id" /]
[s2Get user_option="s2member_last_payment_time" /]
[s2Get user_option="s2member_auto_eot_time" /]
[s2Get user_option="any other meta_key" /]

Statistics: Posted by Jason Caldwell — August 30th, 2010, 9:50 am


]]>
2010-08-28T22:00:12-05:00 http://www.primothemes.com/forums/viewtopic.php?t=627&p=2637#p2637 <![CDATA[Display custom fields in posts.]]> Statistics: Posted by MikeL — August 28th, 2010, 10:00 pm


]]>