Announcement

Announcement Module
Collapse
No announcement yet.

Manual contribution : filter documentation by category in backend

Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Manual contribution : filter documentation by category in backend

    Hi,

    Maybe I missed something but I cannot find how to filter my documentations posts by categories in the backend.

    I wrote my own functions based on this article of Simon Codrington : https://www.sitepoint.com/customized...ation-filters/

    If your devs can check this and maybe add it into the project, I'll be glad.

    I didn't run a lot of tests, it just meet my actual needs.

    Add this code into wp_content/themes/manual/functions.php

    PHP Code:
    function add_manual_documentation_category_filter_to_post_administration()
    {
        global 
    $post_type;
        if(
    $post_type == 'manual_documentation')
        {

            
    $dropdown_args = array(
                
    'show_option_all'   => 'All Categories',
                
    'orderby'           => 'NAME',
                
    'order'             => 'ASC',
                
    'name'              => 'manualdocumentationcategory_admin_filter',
                
    'taxonomy'          => 'manualdocumentationcategory'
            
    );

            
    //if we have a category already selected, ensure that its value is set to be selected
            
    if(isset($_GET['manualdocumentationcategory_admin_filter']))
            {
                
    $dropdown_args['selected'] = sanitize_text_field($_GET['manualdocumentationcategory_admin_filter']);
            }

            
    wp_dropdown_categories($dropdown_args);

        }
    }
    add_action('restrict_manage_posts','add_manual_documentation_category_filter_to_post_administration');

    //restrict the posts by the chosen documentation category
    function add_manual_documentation_category_filter_to_posts($query)
    {

        global 
    $post_type$pagenow;
        
        
    //if we are currently on the edit screen of the post type listings
        
    if($pagenow == 'edit.php' && $post_type == 'manual_documentation')
        {
            if(isset(
    $_GET['manualdocumentationcategory_admin_filter']))
            {
                
    $manualdocumentationcategory_id sanitize_text_field($_GET['manualdocumentationcategory_admin_filter']);
                
                if(
    $manualdocumentationcategory_id != 0// 0 == all posts
                
    {

                    
    $query->query_vars['tax_query'] = array(
                        array(
                            
    'taxonomy'  => 'manualdocumentationcategory',
                            
    'field'     => 'ID',
                            
    'terms'     => array($manualdocumentationcategory_id)
                        )
                    );
                }
            }
        }
    }
    add_action('pre_get_posts','add_manual_documentation_category_filter_to_posts'); 

  • #2
    Thank you so much for providing the code, we will surely implement this feature on our new upcoming version.
    Best Regards,

    Jabin Kadel
    Founder & CEO
    WpSmartApps.com

    Comment

    Working...
    X