Filter by category in the WordPress custom content type list

Source: Internet
Author: User


This time, I helped a customer in Beijing develop an enterprise application using wordpress, which is very similar to erp. I used wordpress for erp for the first time, and there are still many strange requirements.

For example, in supplier management, I made the supplier a custom content type. Custom categories are also included in the custom type. You need to display a filter for the custom category, such as the default monthly category.

After searching for a long time, I finally found the following code to solve the problem. I used a wordpress action and a filter)

 

The code is as follows: Copy code
Function work_restrict_manage_posts (){
Global $ typenow;
$ Args = array ('public' => true, '_ builtin' => false );
$ Post_types = get_post_types ($ args );
If (in_array ($ typenow, $ post_types )){
$ Filters = get_object_taxonomies ($ typenow );
Foreach ($ filters as $ tax_slug ){
$ Tax_obj = get_taxonomy ($ tax_slug );
Wp_dropdown_categories (array (
'Show _ option_all '=> _ ('all'. $ tax_obj-> label ),
'Taxonomy' => $ tax_slug,
'Name' => $ tax_obj-> name,
'Orderby' => 'term _ order ',
'Selected' => $ _ GET [$ tax_obj-> query_var],
'Hierarchical '=> $ tax_obj-> hierarchical,
'Show _ count' => false,
'Hide _ empty' => false
));
        }
    }
}
Function work_convert_restrict ($ query ){
Global $ pagenow;
Global $ typenow;
If ($ pagenow = 'edit. Php '){
$ Filters = get_object_taxonomies ($ typenow );
Foreach ($ filters as $ tax_slug ){
$ Var = & $ query-> query_vars [$ tax_slug];
If (isset ($ var) & $ var> 0 ){
$ Term = get_term_by ('id', $ var, $ tax_slug );
$ Var = $ term-> slug;
            }
        }
    }
Return $ query;
}
Add_action ('restrict _ manage_posts ', 'work _ restrict_manage_posts ');
Add_filter ('parse _ query', 'work _ convert_restrict ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.