admin管理员组

文章数量:1425772

I have written code that filters products by manufacturer. On one website this code works on second not working ... can you help me with this? After filtorvani products website take one product even though the manufacturer has 158 products.

function filter_by_manufacture() {
    $params = array(
        'name' => 'vyrobca',
        'show_option_all' => 'Vyberte výrobcu',
        'taxonomy'        => 'vyrobca',
        'show_count'         => 1,
        'value_field'      => 'slug'
    );
    if ( isset($_GET['vyrobca']) )
        $params['selected'] = $_GET['vyrobca'];

    if ( get_post_type() == 'product' ) {
        wp_dropdown_categories( $params );
    } 
}
add_action('restrict_manage_posts', 'filter_by_manufacture');

I don't understand why the code shouldn't work

I have written code that filters products by manufacturer. On one website this code works on second not working ... can you help me with this? After filtorvani products website take one product even though the manufacturer has 158 products.

function filter_by_manufacture() {
    $params = array(
        'name' => 'vyrobca',
        'show_option_all' => 'Vyberte výrobcu',
        'taxonomy'        => 'vyrobca',
        'show_count'         => 1,
        'value_field'      => 'slug'
    );
    if ( isset($_GET['vyrobca']) )
        $params['selected'] = $_GET['vyrobca'];

    if ( get_post_type() == 'product' ) {
        wp_dropdown_categories( $params );
    } 
}
add_action('restrict_manage_posts', 'filter_by_manufacture');

I don't understand why the code shouldn't work

Share Improve this question edited May 31, 2019 at 12:53 LoicTheAztec 3,39117 silver badges24 bronze badges asked May 31, 2019 at 5:32 uhercikuhercik 132 silver badges5 bronze badges 2
  • this problem make plugin "WooCommerce Search by Product SKU" sk.wordpress/plugins/woo-search-by-product-sku – uhercik Commented May 31, 2019 at 6:28
  • Any chance to get some feed back on the answer please, Thank you. – LoicTheAztec Commented Aug 7, 2019 at 1:00
Add a comment  | 

1 Answer 1

Reset to default 1

There is some missing parts in your code:

Try the following:

add_action('restrict_manage_posts', 'admin_products_by_manufacturer_filter_dropdown');
function admin_products_by_manufacturer_filter_dropdown() {
    global $typenow, $pagenow;

    $taxonomy = 'vyrobca'; // The custom taxonomy

    if( 'edit.php' === $pagenow && 'product' === $typenow && taxonomy_exists( $taxonomy ) ) {
        $info_taxonomy = get_taxonomy($taxonomy);
        $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';

        wp_dropdown_categories( array(
            'show_option_all' => sprintf( __("Zobraziť všetkých %s", "woocommerce"), $info_taxonomy->label ),
            'taxonomy'        => $taxonomy,
            'name'            => $taxonomy,
            'selected'        => $selected,
            'orderby'         => 'name',
            'show_count'      => true,
            'hide_empty'      => true,
        ) );
    }
}

add_action('parse_query', 'admin_products_by_manufacturer_filter_query');
function admin_products_by_manufacturer_filter_query( $query ) {
    global $typenow, $pagenow;

    $taxonomy = 'vyrobca'; // The custom taxonomy

    if ( 'edit.php' === $pagenow && 'product' === $typenow && taxonomy_exists( $taxonomy ) ) {
        $q_vars = &$query->query_vars;

        if ( isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
            $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
            $q_vars[$taxonomy] = $term->slug;
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). It should works.

本文标签: Filter WooCommerce admin products list by a custom taxonomy