Automatic Sort for all new future created categories

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

Sorting post types within Taxonomies Terms become an easy task through Advanced Post Types Order plugin. Further more the Automatic Sort functionality put the code on work and make the sort for you per predefined criteria https://www.nsp-code.com/advanced-post-types-order-description-and-usage/manual-automatic-sorting-type-usage/

The Automatic Sort is an efficient way to improve site management productivity by reduce redundant sort actions. It takes arguments for a particular taxonomy terms and auto-sort any objects inside (previously created or future ones) by that criteria.

What if Automatic Sort is needed to be created automatically once a new taxonomy term (like categories) has been created? This is an easy task, using the Advanced Post Types Order plugin API’s.

First we add an action which trigger when a specific taxonomy term is being created. For this particular example i use product_category taxonomy name. This should be replaced with your own taxonomy name or use the ‘created_term’ action instead which trigger on any term insert.

// create a sort view when a new term is created
add_action( "created_what_you_got_category", "custom_created_what_you_got_category");

At this point you should have already a sort list set-up which does not use any sort queries filtering https://www.nsp-code.com/advanced-post-types-order-description-and-usage/create-simple-sort-for-a-custom-post-type/ This will be used for let the code know to where it should append the sort view corresponding to new term.

//load the API
global $APTO;

//A previously created sort id for which we will add a new sort view corresponding to newly created term
$sort_id    =   1931;
$taxonomy   =   'what_you_got_category';

Include the additional APTO interface and define a class out of it. Then create a sort_view of this term

//create an instance of APTO Interface Helper
include_once(APTO_PATH . '/include/apto_interface_helper-class.php');
$APTO_interface_helper  =   new APTO_interface_helper();

//check what kind of sort we deal with, a multiple or simple (focused)
$sort_view_type  =    $APTO_interface_helper->get_sort_view_type($sort_id);

//if not multiple, we can't add a new view for this, the sort settings aren't right to be used with that method
if($sort_view_type  !=  'multiple')
    return;

//Check if there's a sort view for this term previously created
//important to set the correct language in case using multilingual plugin. As now it use the default site language
$attr   =   array(
                    '_view_selection'   =>  'taxonomy',
                    '_taxonomy'         =>  $taxonomy,
                    '_term_id'          =>  $term_id,
                    '_view_language'    =>  $APTO->functions->get_blog_language()
                    );
$sort_view_id   =   $APTO->functions->get_sort_view_id_by_attributes($sort_id, $attr); 
if(!empty($sort_view_id))
    return;
    
//create the sort view. 
$sort_view_meta     =   array(
                                '_order_type'               =>  'manual',
                                '_view_selection'           =>  'taxonomy',
                                '_taxonomy'                 =>  $taxonomy,
                                '_term_id'                  =>  $term_id,
                                '_view_language'            =>  $APTO->functions->get_blog_language()
                                );                     
$sort_view_id       =   $APTO_interface_helper->create_view($sort_id, $sort_view_meta);

Create the variable data which held the automatic sorting information’s. These need to be set as arrays and every level correspond to a Fallback Block Applying multiple order criteria for Automatic Order through Fallback functionality

//possible values: _default_, ID, post_author, post_name, post_date, post_modified, comment_count, _random_, _custom_function_, _custom_field_
//For automatic custom field ordering use _custom_field_
//FOr automatic custom ordering by a calback function use _custom_function_ 
$order_by               =   array('ASC');

//the custom field name
$custom_field_name      =   array('custom field_name');

//possible values: NONE, SIGNED, UNSIGNED, FLOAT, DATE, DATETIME, TIME
$custom_field_type      =   array('NONE');

//callback function name
$custom_function_name   =   array('callback_custom_functions_name');

//possible values: ASC, DESC
$order               =   array('ASC');

Add all data within the sort_view post ID meta table.

update_post_meta($sort_view_id, '_auto_order_by', $order_by); 
update_post_meta($sort_view_id, '_auto_custom_field_name', $custom_field_name);
update_post_meta($sort_view_id, '_auto_custom_field_type', $custom_field_type); 
update_post_meta($sort_view_id, '_auto_custom_function_name', $custom_function_name);
update_post_meta($sort_view_id, '_auto_order', $order);

This is the full code in a row

	// create a sort view when a new term is created
    add_action( "created_what_you_got_category", "custom_created_what_you_got_category");
    function custom_created_what_you_got_category($term_id)
        {
            //load the API
            global $APTO;
            
            //A previously created sort id for which we will add a new sort view corresponding to newly created term
            $sort_id    =   1931;
            $taxonomy   =   'what_you_got_category';
                        
            //create an instance of APTO Interface Helper
            include_once(APTO_PATH . '/include/apto_interface_helper-class.php');
            $APTO_interface_helper  =   new APTO_interface_helper();
            
            //check what kind of sort we deal with, a multiple or simple (focused)
            $sort_view_type  =    $APTO_interface_helper->get_sort_view_type($sort_id);
            
            //if not multiple, we can't add a new view for this, the sort settings aren't right to be used with that method
            if($sort_view_type  !=  'multiple')
                return;
            
            //Check if there's a sort view for this term previously created
            //important to set the correct language in case using multilingual plugin. As now it use the default site language
            $attr   =   array(
                                '_view_selection'   =>  'taxonomy',
                                '_taxonomy'         =>  $taxonomy,
                                '_term_id'          =>  $term_id,
                                '_view_language'    =>  $APTO->functions->get_blog_language()
                                );
            $sort_view_id   =   $APTO->functions->get_sort_view_id_by_attributes($sort_id, $attr); 
            if(!empty($sort_view_id))
                return;
                
            //create the sort view. 
            $sort_view_meta     =   array(
                                            '_order_type'               =>  'manual',
                                            '_view_selection'           =>  'taxonomy',
                                            '_taxonomy'                 =>  $taxonomy,
                                            '_term_id'                  =>  $term_id,
                                            '_view_language'            =>  $APTO->functions->get_blog_language()
                                            );                     
            $sort_view_id       =   $APTO_interface_helper->create_view($sort_id, $sort_view_meta);
            
            //set the required automatic sort
            
            //possible values: _default_, ID, post_author, post_name, post_date, post_modified, comment_count, _random_, _custom_function_, _custom_field_
            //For automatic custom field ordering use _custom_field_
            //FOr automatic custom ordering by a calback function use _custom_function_ 
            $order_by               =   array('ASC');
            
            //the custom field name
            $custom_field_name      =   array('custom field_name');
            
            //possible values: NONE, SIGNED, UNSIGNED, FLOAT, DATE, DATETIME, TIME
            $custom_field_type      =   array('NONE');
            
            //callback function name
            $custom_function_name   =   array('callback_custom_functions_name');
            
            //possible values: ASC, DESC
            $order               =   array('ASC');
            
            update_post_meta($sort_view_id, '_auto_order_by', $order_by); 
            update_post_meta($sort_view_id, '_auto_custom_field_name', $custom_field_name);
            update_post_meta($sort_view_id, '_auto_custom_field_type', $custom_field_type); 
            update_post_meta($sort_view_id, '_auto_custom_function_name', $custom_function_name);
            update_post_meta($sort_view_id, '_auto_order', $order);
                        
        }