Wordpress - Add a user as custom field of CPT - wordpress

Intro info:
I have a Custom Post Type (CPT) called Service.
I am already connecting users to this CPT using the plug-in 'Posts 2 Posts', this is a many to many relation, and these users connected to the CPT represent the 'team' of said Service.
Alright so here's the headscratcher:
Now I want to create a custom field in the CPT Service, which will be the Director of the Service. Say a field called 'Director' in which the value is a User.
Ideally, the value of the field Director is a User, not a string representing the user name or anything like that.
I have no idea how to start tackling this issue and some brightness on the subject would be strongly appreciated.
Thank you!

save it as post_meta. e.g if you have the user email:
$user = get_user_by( 'email');
update_post_meta($serviceid, '_director', $user->ID);
If you are setting this up in the admin section you can add a meta box and attach a action to save_posts to save a html field where you select a user. Google add_meta_box, there are a load of tutorials on this.

Related

how I can let shop manger read and write only one field in woo commerce

I am trying to let shop manger access (read & write) only one field on order detail dishoard.
How I can disable other field?
There is a function in wordpress which manage to select very detailed permissions: https://pl.wordpress.org/plugins/user-role-editor/
There is Plan B, I call it Jewish Method: There is a function in wordpress get_role( string $role ). You can edit function when these fields are being rendered for individual roles

Drupal how to have certain admins only see the users they've created

I use Drupal 7 and would prefer to do so but if I need to use Drupal 6 I will. I have a Drupal 7 site that I allow "advisors" to create authenticated users. I am an admin on the site so I see and can do everything. All that an "advisor" can do is create and edit authenticated users. Is there a way to make a view that displays the users that the particular advisor has created while the advisor is logged in?
If you don't understand what I'm saying let me put it this way. I am the admin of the site so I can do everything. I created a user role called advisor. There's also an authenticated role for users. Advisors can create authenticated users. So I have Advisor 1. Advisor 1 created 10 users. I also have Advisor 2. Advisor 2 created 3 users. I'd like a page (more than likely built with views - and I've used views before on other sites so I'm familiar with them) - I'd like a page that Advisor 1 could go to once logged in and it would display all of the 10 users that they created. Advisor 1 wouldn't see the users that Advisor 2 create.
Is this possible? Any help on this would be greatly appreciated. Thank you in advance.
I have implemented something similar and I can give you some guidelines.
First of all drupal 7 doesn't store the information of the creator of the users.
So, in order to track this you will need to attach a custom field to the user to keep this information.
In such case you ll have to make sure that every creator can insert only himself in this field and not anyone else or you 'll have to find an automated way to fill it in.
I suggest you use field permission module for setting permissions to this field (users probably should not even have view, creators should not be able to change it etc ) and computed field module for automatically populate the field (eg creator_field) upon creation for example with the uid of the creator.
Finally when creating the view you could add contextual filtering by using the logged in user on the creator_field.
You should also have a look at Organic Groups. I haven't use it but might be helpful
Hope it helps.
Updated
Add
global $user;
$entity_field[0]['value'] = $user -> uid;
to the Computed Code (PHP) in the computed field settings and store the value as integer in "Database store settings". Then in your view you should add a view of user with contextual filter of creator_field. In the contextual filter settings you must set "provide default value" -> "User ID from logged in user" in the "WHEN THE FILTER VALUE IS NOT IN THE URL" section.
This could be done relatively easily in Drupal 7 by adding a field to the user profile that points back to the user creator (using the Entity Reference module). Profile2 could offer a shortcut to making this field available on a profile, though if this is the only customization that you need to add to your profiles, it would be cleaner to do this in a custom module.
A view (Views module) could be configured to output a list of users with the current logged in user as the creator.
You'll also need to add similar logic for user_access to allow/restrict profile editing (if the current user is the user referenced in the profile). A permissions hook also could be useful if you plan to have different admin levels.
The Tokens module should work to insert this value for the new user, by inserting the current user into the field. Or you can do this before the user is saved. It would go a little something like this:
function mymodule_user_presave(&$edit, $account, $category) {
if ($account->is_new) {
global $user;
$created_by = $user->uid;
$edit['created_by'] = $created_by;
}
}
Good luck.

WP_Insert_Post change status depending on previous submissions

I have created a frontend form that allows logged in users to submit content to my wordpress site. The form allows them to submit to a custom content type. I would like be able to change the status of the submitted post depending on that users previous submissions. So if they already have 1 or more published posts then the submission goes to publish - if they don't then it goes to pending.
I know the argument that I need to change: 'post_status' => 'publish' - I just don't really have a clue how to start on the logic - let alone what the most efficient way of achieving it will be...?
first, set post_status as a PHP variable that is set by a function, and hard coded returned from within the function.
Then, refactor the function to do a separate WP_Query to find the count of published custom post types with the current userid as author. If >=1, return 'publish' else return 'draft'

Drupal subscriptions to taxonomy only

Playing around with the subscriptions module; i have some troubles getting it to send the right notification for right subscription.
Here's the situation :
I have a content-type of type 'work'; it has a cck-taxonomy field; when creating the content users choses one category in which his 'work' fulfills.
In user profil, under Categories (user/3/subscriptions/taxa) I choose two categories, lets say 'house work' and 'car work'.
When creating a new 'work' content I do not get the notification.
But, when manually select 'content-type' in user's profile of type 'work' I get the notification e-mail but independant to which 'category' i had chosen.
This is quite annoying since I only want the user to receive his notifications upon the taxonomy he has chosen, not for every new content of type 'work'.
Am I missing something obvious here ?
i have made it working fine for a taxonomy using the settings provided by the module. Concerning the rights i only allowed 'Subscribe to taxonomy terms '

Drupal: help with content type creating & linking nodes

I have a situation where someone can create a job content type. I've added a CCK field that is related to the 'organisation' content type. So when someone creates a new job posting they can choose which organisation the job is for in that field (auto completed). The problem is I can create a new job and select any organisation, even if I didn't create that organisation myself.
Is there a way I can restrict what someone types in that field to 1. an organisation name that exists, and 2. an organisation that belong to that user?
I'm using drupal 6.
Any help most appreciated!
Jonesy
You can use a php rule for the allowed values array of your node reference field, which I think in your case would need to run a quick query for all the nodes made by the current user.
I have not tested this code, but it should be reasonably close to what you're after if I have understood the question!
global $user; //make sure we have access to the user
//find all the job organisation nodes made by this user
//(replace organisation with the actual name of your content type)
$query = db_query("select nid from {node} where type = 'organisation' and uid = %d", $user->uid);
//put the results in the values array
$values = array()
while($result = db_fetch_result($query)){
$values[] = $result;
}
I'm not in a place where I can check this easily, but you could also try using a select on the edit page, and display the choices with a view. Then, create a view over the content type and check that the node user is the logged in user.
You could use a Nodereference field.
It's Autocomplete, and on the bottom of the settings of the field, you can select a View to filter the Nodes that can be referenced ( it's called "Advanced - Nodes that can be referenced (View) ")
Just make a View limiting the nodes to nodes that the current logged in user created, and use that on the Nodereference settings page.
Using that option, drops the option 'Content types that can be referenced' which is located above the Advanced settings.
So make sure to add a filter on node type.
I assume you already use Nodereference, and you might have looked past that option

Resources