I have to do something but I just can't come up with a way to do it.
On one hand I have created pages who are companies. So every page is a company-page.
On the other hand I have created users who have a custom field added to their profile that holds the name of the company they work at.
No this is merely a connection between those two. So I came to the conclusion that if a company is deleted the users should be to, or at least a message should appear that the company can't be deleted because there are still users that are connected to the company.
I think I should hook in some sort of function to add an action to the page-delete button.
It's almost like if you delete a user and WP asks you if the posts the user has made are assigned to another user.
Can anybody tell me more about this or share some thoughts/ideas?
===================================
UPDATE
So I created the following for a custom_function to hook in the before_delete_post
NOTE: this is tested on a company page for output purposes. I use a $_GET["company"]
request because the name is passed on in the URL.
global $post;
$company = $_GET['company'];
$employees = $wpdb->get_results("
SELECT meta_value
FROM $wpdb->usermeta
WHERE meta_key = 'company'
AND meta_value='$company'"
);
/*
output $employees:
[0] => stdClass Object
(
[meta_value] => company_name
)
*/
if(in_array($company, $employees)){
echo 'Yes';
}else{
echo 'No';
}
Let's say I want to delete the page company_name
. What I do is see if this name is in the usermeta
table. If this is true the delete function should not be executed. If not the page can be deleted.
But this just keeps echo-ing No
while it should echo Yes
because I know the pagename is company_name
and it is in the employee list. So I should have a match.
posts
andpost_meta
tables. The users are stored in theusers
anduser_meta
tables. I added a field to theuser_meta
table that contains the exact same name as the page-name (this is the company name)