Viewing 3 replies - 1 through 3 (of 3 total)
  • yliharma

    (@yliharma)

    Hello!
    I have the same problem…did you find anything on this bug?

    yliharma

    (@yliharma)

    This plugin seems abandoned…therefore I modified the line 548 in restrict-cateories.php which caused the bug:

    Original code in function “posts_query”:

    // Build an array for the categories
    $cat_list_array = explode( ',', $this->cat_list );

    Modified code:

    // Build an array for the categories, including default option "0"
    $cat_list_array = array_merge(array('0'), explode( ',', $this->cat_list ));

    If you don’t want to modify the plugin source then you can add this action in your theme functions:

    add_action('pre_get_posts', 'restrict_categories_fix', 20);
    function restrict_categories_fix($query) {
        // fix bug in Restrict Categories plugin which changes default selected category in post admin list
        global $post_type, $pagenow;
    
        if($pagenow == 'edit.php' && $post_type == 'post') {
            $cat_list_array = $query->get('category__in');
            if (!in_array('0', $cat_list_array)) {
                $cat_list_array = array_merge(array('0'), $cat_list_array);
                $query->set( 'category__in', $cat_list_array );
            }
        }
    }
    yliharma

    (@yliharma)

    Sorry I made a mistake, here is the correct version:

    add_action('pre_get_posts', 'restrict_categories_fix', 20);
    function restrict_categories_fix($query) {
        // fix bug in Restrict Categories plugin which changes default selected category in post admin list
        global $post_type, $pagenow;
    
        if($pagenow == 'edit.php' && $post_type == 'post') {
            $cat_list_array = $query->get('category__in');
            if (count($cat_list_array) && !in_array('0', $cat_list_array)) {
                $cat_list_array = array_merge(array('0'), $cat_list_array);
                $query->set( 'category__in', $cat_list_array );
            }
        }
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Default category is changed’ is closed to new replies.