I've been trying to figure out the solution for the following scenario. I've got a custom post type - job_listing
using the WP Job Manager plugin.
What I am trying to achieve is to change the default user role depending on the job_listing
post count.
So by default, the user's role is 'subscriber'
, but whenever the user submits a job listing, this user role should automatically change to 'employer'
.
I put together a piece of code collected from various tuts based on this. But i still can't make this thing working.
Here is my code in the functions.php:
add_action ('publish_post', 'update_roles');
function update_roles ($post_type = 'job_listing', $post_status = 'publish') {
global $wpdb;
$author = wp_get_current_user();
$query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $author->ID AND post_type = '$post_type' AND post_status = '$post_status'";
$count = $wpdb->get_var($query);
if($count > 0 && current_user_can('subscriber'))
{
// Remove role
$author->remove_role( 'subscriber' );
// Add role
$author->add_role( 'employer' );
}
}
What is it that i'm doing wrong there?