• Resolved woorooo

    (@woorooo)


    Hi,

    Is there an easy way to make UWP user list in admin area sortable by GD Listings count?

    Something similar to this snippet which makes the same for regular post, but would be great also to have a snippet for GD lisings count as well.

    add_filter('manage_users_sortable_columns', 'my_user_sortable_columns');
    function my_user_sortable_columns($sortable_columns) {
        $sortable_columns['posts'] = 'post_count';
        return $sortable_columns;
    }

    Thank you.

    • This topic was modified 3 years, 9 months ago by woorooo.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello!

    This got us curious and we did look into it and it isn’t a perfect fit for GD because GD has an unlimited number of post types, but here is something to get you started

    function gd_snippet_220829_sort_gd_posts_count( $sortable_columns ) {
    	$sortable_columns['gd_listings'] = 'gdpost_count';
    
    	return $sortable_columns;
    }
    add_filter( 'manage_users_sortable_columns', 'gd_snippet_220829_sort_gd_posts_count', 20, 1 );
    
    function gd_snippet_220829_pre_user_query( $user_query ) {
    	global $wpdb;
    
    	if ( ! empty( $user_query->query_vars['orderby'] ) && $user_query->query_vars['orderby'] == 'gdpost_count' ) {
    		$post_types = geodir_get_posttypes();
    		$statuses = array();
    
    		foreach ( $post_types as $post_type ) {
    			$statuses = array_merge( $statuses, array_merge( geodir_get_post_stati( 'posts-count-live', array( 'post_type' => $post_type ) ), geodir_get_post_stati( 'posts-count-offline', array( 'post_type' => $post_type ) ) ) );
    		}
    
    		$user_query->query_from = "FROM {$wpdb->users} LEFT OUTER JOIN ( SELECT post_author, COUNT(*) as gdpost_count FROM {$wpdb->posts} WHERE post_type IN( '" . implode( "', '", $post_types ) . "' ) AND post_status IN( '" . implode( "', '", array_unique( array_filter( $statuses ) ) ) . "' ) GROUP BY post_author ) p ON ( {$wpdb->users}.ID = p.post_author ) ";
    		$order = ! empty( $user_query->query_vars['order'] ) && strtoupper( $user_query->query_vars['order'] ) == 'ASC' ? 'ASC' : 'DESC';
    		$user_query->query_orderby = "ORDER BY gdpost_count {$order}, user_login ASC";
    	}
    
    	return $user_query;
    }
    add_action( 'pre_user_query', 'gd_snippet_220829_pre_user_query', 20, 1 );
    Thread Starter woorooo

    (@woorooo)

    That worked, thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Sortable GD Listings Column in Admin’ is closed to new replies.