Custom post type taxonomy not sorting correctly

i created a custom post type and also some taxonomy to be able to perform sorting on the display list.

It is all working well on my local testing installation but as soon as i put on the production website (where there is some other plugins etc installed compared to my local), then the sorting is not working anymore, any idea ?

Here some part of the plugin code related to my taxonomy and custom post type :

add_action('init', 'students_init');
function students_init(){

    $labels = array(
        'name' = 'Students',
        'singular_name' = 'Student',
        'add_new' = 'Add a student',
        'add_new_item' = 'Add a new student',
        'edit_item' = 'Edit a student',
        'new_item' = 'New student',
        'view_item' = 'See the student',
        'search_items' = 'Search a student',
        'not_found' = 'No students',
        'not_found_in_trash' = 'No students in the trash',
        'parent_item_colon' = '',
        'menu_name' = 'Students',
    );

    register_post_type('student', array(
        'public' = true,
        'publicly_queryable' = false,
        'labels' = $labels,
        'menu_position' = 2,
        'capability_type' = 'post',
        'supports' = false,
    ));

    //add_image_size('slider', 1000, 300, true);

    register_taxonomy( 'activity', 'student', array( 'hierarchical' = true, 'label' = 'Activity', 'query_var' = true, 'rewrite' = true, 'show_admin_column' = true, ) );

}

add_action( 'restrict_manage_posts', 'students_restrict_manage_posts' );
function students_restrict_manage_posts() {
    global $typenow;

    if ( $typenow == 'student' ) 
    {

        $taxonomy = 'activity'; 
        wp_dropdown_categories(array(
            'show_option_all' =  'Voir toutes les catégories',
            'taxonomy'        =  $taxonomy,
            'name'            =  $taxonomy,
            'orderby'         =  'name',
            'selected'        =  $_GET[$taxonomy],
            'hierarchical'    =  true,
            'show_count'      =  true,
            'hide_empty'      =  true
        ));
    }
}

add_action( 'request', 'students_admin_request' );
function students_admin_request( $request ) {

    if ( is_admin()  isset( $request['post_type'] )  $request['post_type'] == 'student' )
    {
        $taxonomy = 'activity';
        $request[$taxonomy] = get_term_by( 'id', $request[$taxonomy], $taxonomy)-slug;

    }
    return $request;
}

Here what i have :

here what i get when i filter :

Thanks a lot in advance for your help :)

Topic custom-taxonomy custom-post-types sort Wordpress

Category Web


If it works localy, Disable the plugins installed on production website to rule out a conflict with those. You can use this plugin to test disabling them without disturbing users of the website if it is live

https://wordpress.org/plugins/health-check/


You should use the action hook pre_get_posts which WordPress uses to set up the entire query.

add_action('pre_get_posts', function($query) {

    // do not exectute if outside admin
    if( !is_admin() )
        return;

    // only do stuff to the main query
    if( !$query->is_main_query() )
        return; 

    // only execture on post type
    if( get_current_screen()->post_type !== 'student' )
        return;

    // no need to filter if not our taxonomy
    if( !isset($_REQUEST['activity']) )
        return;

    $query->set('tax_query', array(
        array(
            'taxonomy'      => 'activity',
            'field'         => 'term_id',
            'terms'         => $_REQUEST['activity'],
        )
    ));
});

For the sorting, you are missing how you want to sort it.

Use

'order' => 'ASC'

or

'order' => 'DESC'

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.