admin管理员组

文章数量:1122832

I display a column from my pods table in the admin list, rendering it sortable and searchable. Each part of code is working fine. But when I search in that column and found the result, I cannot sort it any more: No result found and the list is empty Any idea how to solve this?

Here is my searching code:

function catalog_admin_search ( $join ) {
        global $pagenow, $wpdb;
        if ( is_admin() && 'edit.php' === $pagenow && 'catalog' === $_GET['post_type'] && ! empty( $_GET['s'] ) ) {    
            $join .= 'LEFT JOIN tableOne_catalog ON tableOne_posts.ID = tableOne_catalog.id';
        }
        return $join;
    }
    add_filter( 'posts_join', 'catalog_admin_search' );
    
    
    function catalog_search_where( $where ) {
        global $pagenow, $wpdb;
        if ( is_admin() && 'edit.php' === $pagenow && 'catalog' === $_GET['post_type'] && ! empty( $_GET['s'] ) ) {
            $where = preg_replace(
                "/\(\s*tableOne_posts.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(tableOne_posts.post_title LIKE $1) OR (tableOne_catalog.reference LIKE $1)", $where );
        }
        return $where;
    }
    add_filter( 'posts_where', 'catalog_search_where' );

As I use pods and a separate table, I filter via posts_clauses instead of pre_get_posts.

Here is my sorting code:

   function catalog_sortable_column ( $clauses, $query ) {
        if ( ! is_admin() || ! $query->is_main_query() ) {
            return $clauses;
        }       
        global $wpdb;
        if ( $query->get( 'post_type' ) === 'catalog' && $query->get( 'orderby' ) === 'reference' )  {   
            $clauses['join'] .= " LEFT OUTER JOIN tableOne_catalog ON tableOne_posts.ID = tableOne_catalog.id";
            $clauses['orderby'] = "tableOne_catalog.reference " . $query -> get('order');
        }
        return $clauses;
    }

    add_filter('posts_clauses', 'catalog_sortable_column', 10, 2);

Thanks for your help.

BTW, it sounds to be similar to this question

本文标签: Combining search and sort in the admin list using pods