Support MB Admin Columns MB Admin Columns with MB Relationships Reply To: MB Admin Columns with MB Relationships

#9030
Anh TranAnh Tran
Keymaster

Hello,

The last snippet seems to make the dropdown works, e.g. making the column *filterable*, instead of *sortable*, doesn't it?

If so, can you try this:

add_filter( 'parse_query', 'query_filter_assembly_steps_by_bunkie' );
function query_filter_assembly_steps_by_bunkie( $query ) {
    global $pagenow;
    $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
    if ( is_admin() && $pagenow === 'edit.php' && $post_type === 'assembly_step' && isset( $_GET['bunkie_id'] ) && $_GET['bunkie_id'] !== 'all' ) {
        $query->set( 'p', intval( $_GET['bunkie_id'] ) );
    }
}

This code filters the list and displays only 1 post (the post that is selected).

If you want to filter the list and display all posts that have connected "from" a bunkie, then you might want to use this:

add_filter( 'parse_query', 'query_filter_assembly_steps_by_bunkie' );
function query_filter_assembly_steps_by_bunkie( $query ) {
    global $pagenow, $wpdb;
    $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
    if ( is_admin() && $pagenow === 'edit.php' && $post_type === 'assembly_step' && isset( $_GET['bunkie_id'] ) && $_GET['bunkie_id'] !== 'all' ) {
        $bunkie_id = $_GET['bunkie_id'];
        $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT <code>to</code> FROM $wpdb->mb_relationships WHERE <code>from</code> = %d", $bunkie_id ) );
        $query->set( 'post__in', $post_ids );
    }
}