Hide private posts on woocommerce category page

I am trying to modify the product loop from my functions.php file to exclude private posts from product category pages when admin users are logged in. I have found a number of approaches through a bit of searching but am struggling to modify the below code to achieve what I am after. –

add_filter('posts_where', 'no_privates');
function no_privates($where) {
    if( is_admin() ) return $where;

    global $wpdb;
    return " $where AND {$wpdb->posts}.post_status != 'private' ";
}

This works great but I am struggling to modify it to allow Admins to preview the posts or view the individual products pages for editing and publishing. I was hoping someone could help me modify the code to get it working. Sorry if this is a simple solution, still at the early stages of learning php 🙂

Read More

Cheers

Rich

Related posts

Leave a Reply

1 comment

  1. Ok found a solution I think works, obviously the standard loop for non-logged in users ignores private posts so just changed my code above to query if it is a product category and seems to work.

    add_filter('posts_where', 'no_privates');
    function no_privates($where) {
        if ( ! is_product_category()) return $where;
    
        global $wpdb;
        return " $where AND {$wpdb->posts}.post_status != 'private' ";
    }