using custom meta user data to run queries in WordPress

I have added custom user meta data to the WordPress user profile below is the code I used to add this data. This part of the code seems to be working fine and I do not have a problem with it.

<?php

////////////////////////////////////////////////////////////////////
// Adding a Field to WordPress User Profile
// http://scriptbaker.com/adding-custom-fields-to-wordpress-user-profile-
and-add-new-user-page/
////////////////////////////////////////////////////////////////////

function custom_user_profile_fields($user){
if(is_object($user))
{
$company = esc_attr( get_the_author_meta( ‘company’, $user->ID ) );
$mf_group = esc_attr( get_the_author_meta( ‘mf_group’, $user->ID ) );
}
else
{
$company = null;
$phone = null;
$insurens = null;
}
?>
<h3>Extra profile information</h3>
<table class=”form-table”>
<tr>
<th>
<label for=”company”>Company Name</label>
</th>
<td>
<input type=”text” class=”regular-text” name=”company” value=”<?php echo $company; ?>” id=”company” /><br />
</td>

<th>
<label for=”phone”>MF Group</label>
</th>
<td>
<input type=”text” class=”regular-text” name=”mf_group” value=”<?php echo $mf_group; ?>” id=”mf_group” /><br />
</td>
</tr>
</table>

<?php
}
add_action( ‘show_user_profile’, ‘custom_user_profile_fields’ );
add_action( ‘edit_user_profile’, ‘custom_user_profile_fields’ );
add_action( “user_new_form”, “custom_user_profile_fields” );

function save_custom_user_profile_fields($user_id){
# again do this only if you can
if(!current_user_can(‘manage_options’))
return false;

# save my custom field
update_user_meta($user_id, ‘company’, $_POST[‘company’]);
update_user_meta($user_id, ‘mf_group’, $_POST[‘mf_group’]);
}
add_action(‘user_register’, ‘save_custom_user_profile_fields’);
add_action(‘profile_update’, ‘save_custom_user_profile_fields’);

////////////////////////////////////////////////////////////////////
// add columns to User panel list page
////////////////////////////////////////////////////////////////////

function add_user_columns($column) {
$column[‘company’] = ‘Company’;
$column[‘mf_group’] = ‘MF Group’;

return $column;
}
add_filter( ‘manage_users_columns’, ‘add_user_columns’ );

//add the data
function add_user_column_data( $val, $column_name, $user_id ) {
$user = get_userdata($user_id);

switch ($column_name) {
case ‘company’ :
return $user->company;
break;
default:
}
switch ($column_name) {
case ‘mf_group’ :
return $user->mf_group;
break;
default:
}
return;
}
add_filter( ‘manage_users_custom_column’, ‘add_user_column_data’, 10, 3 );

?>

The problem comes when I want to use this metadata to run queries. Below is a example of where I would like to use this meta data.

<?php
$current_user = wp_get_current_user();
?>
<div class=”row”>
<div class=”col-md text-center”>
<h2>
Welcome To your Dashboard
</h2>
</div>
</div>
<div class=”row”>
<div class=”col-md text-center bg-success”>
All
<?php
$args = array(
‘post_type’ => ‘maintenances’,
‘post_status’ => ‘published’,
‘taxonomy’ => ‘maintenance-type’,
‘term’ => ‘All’,
‘numberposts’ => -1,
‘author’ => $current_user->ID,
‘company’ => $current_user->company,
‘mf_group’ => $current_user->mf_group,
);
$num_1 = count( get_posts( $args ) );
$sum_total = $num_1 / $num_1 * 100;
?>
<h2 class=”align-middle”>
<?php echo $num_1 ;?><br />
100 %<br />
</h2>

////////////////////////////////////////////////////////////////

‘company’ => $current_user->company,

‘mf_group’ => $current_user->mf_group,

///////////////////////////////////////////////////////////////////

this line of code does not seem to be working

if I would Echo the two lines like this

$client = $current_user->company;

$mfgroup = $current_user->mf_group;
//////////////////////////////////////<br/>

Cleint <strong><?php echo $client ?></strong><br/>

Mf Group <strong><?php echo $mfgroup ?></strong><br/>

///////////////////////////////////////<br/>

it does return the correct values from the database

Could Somebody please advise me on the way forward into using this Mega data to run queries thank you

Read more here:: using custom meta user data to run queries in WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *