Search
 
SCRIPT & CODE EXAMPLE
 

PHP

auto check a category when creating new post

add_filter('wp_terms_checklist_args', 'WPSE_pre_select_categories', 10, 2);
function WPSE_pre_select_categories($args, $post_id) {
        $post = get_post($post_id);

        // only pre select categories for new posts
        if ($post->post_status !== 'auto-draft' || $post->post_type !== 'post')
                return $args;

        // select categories with ID 4 and 6
        $select_categories = [4, 6];

        // little hack so array_merge() works if default is NULL
        if (empty($args['selected_cats'])) {
                $args['selected_cats'] = [];
        }

        // array_merge() with numerical indices only appends
        // so I use array_unique() to remove any duplicates
        $args['selected_cats'] = array_unique(array_merge($args['selected_cats'], $select_categories));
        return $args;
}
Comment

PREVIOUS NEXT
Code Example
Php :: laravel find user by id 
Php :: retrieve the order Id on Order pay page 
Php :: joomla 404 
Php :: laravel disable cors 
Php :: laravel left join count 
Php :: current tab active on page reload in php 
Php :: PHP Superglobal - $_REQUEST 
Php :: laravel yajra datatable Add Column with View 
Php :: connexion sql php/html 
Php :: search posts by post title in worpress 
Php :: QR CODE FROM CAMCODES 
Php :: how to convert number into million and billion suffix in PHP using brick/Money package 
Php :: Metabox Array 
Php :: Undefined array key after unset() 
Php :: keep value after submit php 
Php :: why php $_session in not working in react js 
Php :: buddypress groups dropdown 
Php :: Print all before characters once string found | matched string return 
Php :: php accounting ledger 
Php :: laravel sql illegal collation 
Php :: create associative array php by key value site:stackoverflow.com 
Php :: php type generic object 
Php :: laravel jobs cache file picking old job file 
Php :: Posting file in Database comes with unwanted quotation marks laravel 
Php :: .phtml 
Php :: word limit in php 
Php :: how to change directory in command processor 
Php :: php input seperated by space 
Php :: selecting a time zone from a drop-down list 
Php :: howto+add+header+bar+laravel+app 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =