function my_custom_product_filters( $post_type ) {
$value1 = '';
$value2 = '';
if( isset( $_GET['my_filter'] ) ) {
switch( $_GET['my_filter'] ) {
case 'value1':
$value1 = ' selected';
break;
case 'value2':
$value2 = ' selected';
break;
}
}
if( $post_type == 'product' ) {
$terms = get_terms("pa_proizvoditel");
$i=0;
foreach ( $terms as $term ) {
$im[$i]=$term->name;
$i=$i+1;
}
echo '<select name="my_filter">';
echo '<option value>Производители</option>';
echo '<option value="' . $im[0] . '"' . $value1 . '>' . $im[0] . '</option>';
echo '<option value="' . $im[1] . '"' . $value2 . '>' . $im[1] . '</option>';
echo '</select>';
}
}
add_action( 'restrict_manage_posts', 'my_custom_product_filters' );
function apply_my_custom_product_filters( $query ) {
global $pagenow;
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['my_filter'] ) && $_GET['my_filter'] != '' && $_GET['post_type'] == 'product' ) {
$meta_key_query = array(
array(
'key' => '_my_meta_value',
'value' => esc_attr( $_GET['my_filter'] ),
)
);
$query->set( 'meta_query', $meta_key_query );
}
}
add_action( 'pre_get_posts', 'apply_my_custom_product_filters' );