Created
April 28, 2025 02:00
-
-
Save shameemreza/ef6b17dfc1fb1886465e6312b8744268 to your computer and use it in GitHub Desktop.
Adds a "No Vendor" filter link to the WooCommerce Products admin screen for stores using WooCommerce Product Vendors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add custom view for products without a vendor | |
add_filter( 'views_edit-product', function( $views ) { | |
$class = ( isset($_GET['no_vendor']) && $_GET['no_vendor'] == 1 ) ? 'current' : ''; | |
$url = add_query_arg( 'no_vendor', '1', admin_url( 'edit.php?post_type=product' ) ); | |
$views['no_vendor'] = "<a href='{$url}' class='{$class}'>No Vendor</a>"; | |
return $views; | |
}); | |
// Modify the query to show products without vendor when selected | |
add_action( 'pre_get_posts', function( $query ) { | |
if ( !is_admin() || !$query->is_main_query() ) { | |
return; | |
} | |
if ( isset( $_GET['no_vendor'] ) && $_GET['no_vendor'] == 1 ) { | |
$query->set( 'tax_query', array( | |
array( | |
'taxonomy' => 'wcpv_product_vendors', | |
'operator' => 'NOT EXISTS' | |
) | |
)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment