• Hi guys,

    I have been struggling a couple of hours combining both actions into one single code; since they do not work together. Can someone please instruct me how to make one single filter?

    /* #### Nieuw filter #### */
     /* Filteren op gearchiveerde producten (back-end)*/
    add_action( 'restrict_manage_posts', 'filter002_admin_posts_filter_restrict_manage_posts' );
    
    function filter002_admin_posts_filter_restrict_manage_posts(){
    
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
    
        if ('product' == $type){
            $values = array(
                'Zichtbaar' => 'visible', 
                'Gearchiveerd' => 'hidden',
            );
            ?>
            <select name="Stock">
            <option value=""><?php _e('Archief', 'filter002'); ?></option>
            <?php
                $current_v = isset($_GET['Stock'])? $_GET['Stock']:'';
                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', 'filter002_posts_filter' );
    
    function filter002_posts_filter( $query ){
        global $pagenow;
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
        if ( 'product' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['Stock']) && $_GET['Stock'] != '') {
            $query->query_vars['meta_key'] = '_visibility';
            $query->query_vars['meta_value'] = $_GET['Stock'];
        }
    }
    /* #### Nieuw filter #### */
    /* Add In/Out of Stock Filter to Admin *aanvulling op WooCommerce Sort By Stock* */
    add_action( 'restrict_manage_posts', 'filter001_admin_posts_filter_restrict_manage_posts' );
    
    function filter001_admin_posts_filter_restrict_manage_posts(){
    
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
    
        if ('product' == $type){
            //change this to the list of values you want to show
            //in 'label' => 'value' format
            $values = array(
                'Uitverkocht' => 'outofstock', 
                'Op voorraad' => 'instock',
            );
            ?>
            <select name="Stock">
            <option value=""><?php _e('Voorraad', 'filter001'); ?></option>
            <?php
                $current_v = isset($_GET['Stock'])? $_GET['Stock']:'';
                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', 'filter001_posts_filter' );
    
    function filter001_posts_filter( $query ){
        global $pagenow;
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
        if ( 'product' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['Stock']) && $_GET['Stock'] != '') {
            $query->query_vars['meta_key'] = '_stock_status';
            $query->query_vars['meta_value'] = $_GET['Stock'];
        }
    }

    Thanks!

    • This topic was modified 7 years, 5 months ago by Kathryn Presner.
    • This topic was modified 7 years, 5 months ago by Kathryn Presner. Reason: put code in code tags - please do this in future, thanks
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[How to?] Combine two actions’ is closed to new replies.