Reputation: 53
i have a custom theme on wordpress that allow users to add posts from the front end the default status for submitted posts is Pending Review i've integrated woocommerce with the theme following the official woocommerce tut now i'm facing an issue of all the pending posts are showing on the frontend like they are published
i was able to show the posts on the frontend using this hook
//Allow Pending products to be viewed by listing/product owner
function allow_pending_listings($qry) {
$edit_data = get_post($_GET['eid']);
if (!is_admin() && $edit_data->post_author == $userdata->ID) {
$qry->set('post_status', array('publish','pending'));
}
}
add_action('pre_get_posts','allow_pending_listings');
what i want to do is to get all posts and show all published posts to all users buy only show pending review posts to the only the posts author
Upvotes: 1
Views: 867
Reputation: 135
that is possible if you get the slug from the query
/**
* ALlow the preview pending for post author
*
* @since 1.0.0
*/
function allow_pending_listings($query) {
// if user connected and single template and front end
if( is_user_logged_in() && is_singular() && !is_admin() ) {
$slug = array_key_exists('name', $query->query) ? $query->query["name"] : null;
//$slug = $query->query["name"] ?? null; // php7 required
// if the slug exist in query
if ( $slug ) {
$post = get_page_by_path( $slug, OBJECT, 'your_post_type' );
// if the post_auhtor is the correct current id
if ( $post->post_author == get_current_user_id() ) {
$query->set( 'post_status', array('publish', 'pending') );
// will add the "pending" status to loop query
}
}
}
}
add_action('pre_get_posts','allow_pending_listings');
Upvotes: 0
Reputation: 53
For those who are looking for an answer, the answer is with woocommerce user roles and capabilities woocommerce took over all the default roles & capabilities to its own management
i used this plugin and modified the customer role to be able to edit my CPT https://wordpress.org/plugins/capability-manager-enhanced/
Upvotes: 1