Skip to content

Instantly share code, notes, and snippets.

@WPprodigy
Created November 16, 2016 22:11
Show Gist options
  • Save WPprodigy/8599452871c9efb042cc18d34f0da571 to your computer and use it in GitHub Desktop.
Save WPprodigy/8599452871c9efb042cc18d34f0da571 to your computer and use it in GitHub Desktop.
Filter products by visibility
add_action( 'restrict_manage_posts', 'wc_ninja_create_filter_dropdown' );
function wc_ninja_create_filter_dropdown(){
$type = 'product';
if ( isset( $_GET['post_type'] ) ) {
$type = $_GET['post_type'];
}
if ( 'product' == $type ){
$values = array(
'Catalog & search' => 'visible',
'Catalog' => 'catalog',
'Search' => 'search',
'Hidden' => 'hidden'
);
?>
<select name="product_visibility">
<option value=""><?php _e('Filter by Visibility ', 'wose45436'); ?></option>
<?php
$current_v = isset( $_GET['product_visibility'] ) ? $_GET['product_visibility'] : '';
foreach ( $values as $label => $value ) {
printf
(
'<option value="%s"%s>%s</option>',
$value,
$value == $current_v ? ' selected="selected"' : '',
$label
);
}
?>
</select>
<?php
}
}
add_filter( 'parse_query', 'wc_ninja_parse_filter_dropdown' );
function wc_ninja_parse_filter_dropdown( $query ){
global $pagenow;
$type = 'product';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'product' == $type && is_admin() && $pagenow=='edit.php' && isset( $_GET['product_visibility'] ) && $_GET['product_visibility'] != '') {
$query->query_vars['meta_key'] = '_visibility';
$query->query_vars['meta_value'] = $_GET['product_visibility'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment