get_users including user meta in the WP_User Object - wordpress

I would like to include in the WP_User Objects, which I get from the get_users function, also my custom meta user, which I create with Advanced Custom Fields.
This is to avoid to make one query per user after I get the array of WP_User Objects from the get_users.
$users_list = get_users( $args );
foreach($users_list as $user){
$u_fields = get_fields('user_'.$user->ID);
}
I have about 500 users and it means 500 query. Is that correct?

Related

How to access WordPress post admin data inside the loop?

I'm trying to access some fields that have been assigned to a post via a plugin named Snax.
You can see here in the admin that "Story" has being assigned to a post as a "Snax Format":
But when I check the $post object inside the loop on the front end using something like $my_post = get_post(); var_dump($my_post); I can't see any anything relating to snax or "story".
What php can I use to access that post data on the front end inside the loop?
I'm asking because I want to use that field data inside my post template.
I think the plugin will store they data as meta_values. You can retrieve this data with get_post_meta()
$meta_data = get_post_meta( get_the_ID() );
var_dump( $meta_data );

How to List all the Form Fields of Contact Form 7 WordPress?

Most of developer while developing a task related to the CF7, they need the type and name of the fields, but generally they failed to grab as the form is stored as a content due to custom post type.
So i can grab all those fields in a proper way?
Most of developer while developing a task related to the CF7, they need the type and name of the fields, but generally they failed to grab as the form is stored as a content due to custom post type.
So to fetch type, name, basetype, etc... you need to fetch all contact form post.
You need to loop through every contact form post and pass the content of the form in below scan function as a agument
foreach($form as $objForm){
$manager = WPCF7_FormTagsManager::get_instance();
$tags = $manager->scan( $objForm->form );
$filter_result = $manager->filter( $tags, $cond );
foreach ($filter_result as $key => $value) {
echo $value->type;
echo $value->name;
}
}

Wordpress ACF Pro & CPT / User Logged In = User in Field Type

I need a little help, if anyone can!
Using ACF Pro, and CPT, I have made a front end form where an admin can choose a user name and other specific information, then click submit and the post is published.
What I want to do now, is only allow the chosen user to see that post as long as they are logged in. Meaning, the chosen user is logged in and can only see the new post that has their name selected.
Thanks in advance! I’ve tried just about everything.
When creating the post, don't store just the username - store the user ID as well as a meta value on the post. Then, for the logged-in user, get the current user get the current user prior to querying your posts, and more specifically, get the user's ID. Then, pass that in your post query to limit the results to just that user's "tagged" posts.
Something like this (assuming we're on an archive, your post type is 'messages'), and you stored the "tagged" user id in a field named tagged_user_id:
add_action( 'pre_get_posts', 'get_users_posts' );
function get_users_posts( $query ){
if( $query->is_main_query() && is_post_type_archive( 'messages' ) && is_user_logged_in() ){
$current_user = wp_get_current_user();
$query->set( 'meta_key', 'tagged_user_id' );
$query->set( 'meta_value', $current_user->ID );
}
}
Doing this in your functions.php file, you ensure that any logged-in user on the 'messages' archive page only sees his/her messages. You can adapt it for single-messages as well, and you'll want to handle users who are not logged in as well (I presume).

WordPress: get all meta data when user registers

I have a user registration form in the front end (in the Users admin section as well) with three extra fields (apart from default ones): birthday, country, language. their values are stored in usermeta table.
I have this action hook to retireve all meta data for the registered user:
add_action('user_register', 'new_user_func');
// user registration callback function
function new_user_func($userID) {
$newUser = get_user_meta( $userID );
$userMeta = array();
foreach ($newUser as $key => $value) {
$userMeta[$key] = $value[0];
}
//do something with $userMeta...
}
var_dump($userMeta) after submit doesn't give me the extra fields value though.. only defaults (first name, last name etc)
Anyone know what might be the case?
Did you try getting the values with:
$meta = get_the_author_meta($meta_key, $user_id);
Perhaps the meta values you add yourself isn't supported by get_user_meta() .
If this don't work either, perhaps you need to look on how you went about creating the new meta fields. Theres a pretty decent tutorial on how to do it here:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
Read de Codex entry for user_register action, it says:
Not all user metadata has been stored in the database when this action is triggered.

Register Author Type user in wordpress

I am using wp_create_user function to register new user in wordpress.
This function is inserting user as a role of subscriber. I want to insert users as a author type role. How can i do this.?
Thanks in advance.
The wp_create_user() function returns the ID of the user it creates. You can then use the WP_User class like:
$new_user_id = wp_create_user($your_args_here);
$u = new WP_User( $new_user_id );
// Remove role
$u->remove_role( 'subscriber' );
// Add role
$u->add_role( 'author' );
Alternatively, you can use wp_update_user() function:
Although I'm not precisely sure how to do that, but I imagine you could work it out by looking in wp-includes/registration.php which is where that function is created.

Resources