0

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

  • User meta belongs to users, not posts. I’m not sure what results you expect querying posts with user meta. – Milo Oct 8 '18 at 13:01
  • The ides it the use the querying posts with user meta to get different results base on the current user role. query one User Role = store querying only the user post .. 'author' => $current_user->ID, query two User Role = area manager querying post made by mf_group. The mf_group has get more than oneuser link to it. .. 'mf_group' => $current_user->mf_group, query three User Role = site manager querying post made by company. 'company' => $current_user->company, – MFWebMaster Oct 8 '18 at 14:15
  • Sorry, still don’t understand. So you’re also storing these values as post meta? company is not a valid argument for get_posts. – Milo Oct 8 '18 at 14:34
0

wp_get_current_user() does not return all the data for the user, you should try using get_user_meta(). Try as below

$company = get_user_meta($current_user->ID,'company');
$mf_group = get_user_meta($current_user->ID,'mf_group');

and in your post loop changes this two lines as below

'company'       => $company[0],
'mf_group'      => $mf_group[0],

Hope this helps.

  • Thank you by not working The ides it the use the querying posts with user meta to get different results base on the current user role. query one User Role = store querying only the user post .. 'author' => $current_user->ID, query two User Role = area manager querying post made by mf_group. The mf_group has get more than oneuser link to it. .. 'mf_group' => $current_user->mf_group, query three User Role = site manager querying post made by company. ..company' => $current_user->company, – MFWebMaster Oct 8 '18 at 14:32
  • @MFWebMaster , still not getting what you wan't to do. – Krishna Joshi Oct 9 '18 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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