1

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.

  • Where do you store user relations for a company? In another table or in post_meta? – Iamzozo Sep 9 '15 at 8:40
  • The company is stored in the posts and post_meta tables. The users are stored in the users and user_meta tables. I added a field to the user_meta table that contains the exact same name as the page-name (this is the company name) – Interactive Sep 9 '15 at 8:50
  • Try to create a WP_User_Query looking for company page name in users meta. In the official docs for meta query: codex.wordpress.org/Class_Reference/WP_User_Query#Examples. In case if company names are not stored in a single record serialized. I did something similar, i solved this by storing connection on both sides. So users have a list which companies they belongs to, and companies which users they have. (company meta, user meta) – Iamzozo Sep 9 '15 at 9:38
  • This is a way to do it. I like it. But how can I do this upon delete of a page? – Interactive Sep 9 '15 at 11:14
  • Add an action, when deleting a post: codex.wordpress.org/Plugin_API/Action_Reference/delete_post You can do the check for relations. If it has any, you can abort the deletion and return with a message. – Iamzozo Sep 9 '15 at 11:44
1

It gives an array with objects. So in_array() won't find anything, you have to convert it before. After you get the results from wbdb:

$employees_array = array();
foreach($employees as $employee) {
    $employees_array[] = $employee->meta_value;
}

if(in_array($company, $employees_array)) {
    ...
}
0

this can be best solved by adding constraints in your database schemes. The best way to achieve this is by adding a foreign key constraint which reacts on the "DELETE" action. For more info about the foreign key read: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

You could also do a SELECT before calling the DELETE action and if it returns users exit the function with a error of some kind.

  • Wow this is (almost) a new one for me... I have never worked with constraints before. I first read up about constraints and then see if it fits my needs :-) . Thnx – Interactive Sep 9 '15 at 8:22

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.