Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created May 22, 2017 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/3e07a0bef89eaaeb901e21f2c384b054 to your computer and use it in GitHub Desktop.
Save tripflex/3e07a0bef89eaaeb901e21f2c384b054 to your computer and use it in GitHub Desktop.
WP Job Manager automatically set post status to publish on submit (if user has over X total posts submitted)
<?php
add_action( 'job_manager_update_job_data', 'smyles_update_job_listing_user_count', 10, 2 );
/**
* Update user meta with total job posts submitted (does not take into account removed or expired listings)
*
*
* @param $job_id
* @param $values
*/
function smyles_update_job_listing_user_count( $job_id, $values ) {
if ( ! $user_id = get_current_user_id() ) {
return;
}
if( ! $total_posts = get_user_meta( $user_id, 'smyles_job_post_count', true ) ){
$total_posts = 0;
}
$new_total_posts = (int) $total_posts + 1;
update_user_meta( $user_id, 'smyles_job_post_count', $new_total_posts );
}
add_filter( 'submit_job_post_status', 'smyles_set_job_post_status_for_user', 10, 2 );
/**
* Automatically set Job post status based on custom conditions
*
* This specifically checks for `smyles_job_post_count` user meta value, and returns publish status
* if user has over X defined posts.
*
* @param $status
* @param $job
*
* @return string
*/
function smyles_set_job_post_status_for_user( $status, $job ){
if( ! $user_id = get_current_user_id() ){
return $status;
}
$required_posts = 10;
$user_posts = get_user_meta( $user_id, 'smyles_job_post_count', true );
// If user has more than required posts submitted, automatically set to published
if( (int) $user_posts > $required_posts ){
return 'publish';
}
// Return passed status
return $status;
}
@tripflex
Copy link
Author

Here's code for setting status based on current user's role:

https://gist.github.com/tripflex/68acbed4d1b3342ae3b73edd2456ec35

@tripflex
Copy link
Author

I used the extra action to add value to user's meta, instead of running WP_Query on all posts to get total count for specific author, which would be more precise, but would require extra queries, so I just used a simple quick method for handling total posts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment