Custom Fields
Re-Envisioned
Support MB Admin Columns MB Admin Columns with MB Relationships

  • Creator
    Topic
  • #9021
    cedriccouret
    Participant

    Hi,

    I’m trying to display a relationship in a custom column and I also need it filterable.
    I managed to display the column with this code:

    
    add_action( 'manage_assembly_step_posts_custom_column' , 'custom_assembly_step_column', 10, 2 );
    function custom_assembly_step_column( $column, $post_id ) {
        switch ( $column ) {
            case 'bunkie' :
                $bunkies = new WP_Query( array(
                    'relationship' => array(
                        'id'   => 'bunkies_to_assembly_steps',
                        'to' => $post_id
                    ),
                    'nopaging' => true,
                ) );
    
                if ($bunkies->have_posts()) {
                    foreach ($bunkies->posts as $index => $bunkie) {
                        $bunkieMetas = get_post_meta($bunkie->ID);
                        echo $bunkieMetas['name_en'][0];
                        if ($index !== count($bunkies->posts) - 1) {
                            echo '<br>';
                        }
                    }
                }
                break;
        }
    }
    

    I’m using this code to display a dropdown with all options:

    
    add_action( 'restrict_manage_posts', 'filter_assembly_steps_by_bunkie' );
    function filter_assembly_steps_by_bunkie() {
        global $typenow;
        global $wpdb;
        if ( $typenow == 'assembly_step' ) {
            $bunkies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'bunkie' AND post_status = 'publish'" );
            $values = [];
    
            foreach ($bunkies as $bunkie) {
                $values[$bunkie->post_title] = $bunkie->ID;
            }
    
            $current_bunkie = '';
            if( isset( $_GET['bunkie_id'] ) ) {
                $current_bunkie = $_GET['bunkie_id'];
            } ?>
            <select name="bunkie_id" id="bunkie_id">
                <option value="all" <?php selected( 'all', $current_bunkie ); ?>><?php _e( 'Filter By Bunkies', 'text_domain' ); ?></option>
                <?php foreach( $bunkies as $bunkie ) { ?>
                    <option value="<?php echo esc_attr( $bunkie->ID ); ?>" <?php selected( $bunkie->ID, $current_bunkie ); ?>><?php echo esc_attr( $bunkie->post_title ); ?></option>
                <?php } ?>
            </select>
        <?php }
    }
    

    But then I don’t how to make it sortable. I was using this code before because I used to set a custom field instead of using your relationships plugin:

    
    add_filter( 'parse_query', 'query_filter_assembly_steps_by_bunkie' );
    function query_filter_assembly_steps_by_bunkie( $query ) {
        global $pagenow;
        // Get the post type
        $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->query_vars['meta_key'] = 'bunkies';
            $query->query_vars['meta_value'] = $_GET['bunkie_id'];
            $query->query_vars['meta_compare'] = '=';
        }
    }
    

    I would really appreciate it if someone could show me how to make this work.
    Thanks !

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #9030
    Anh 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 );
        }
    }
    #9033
    cedriccouret
    Participant

    Hi,

    thanks for your feedback. I’ve used this modified version of your second snippet and was able to filter my assembly steps by bunkie.

    
    $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT to FROM $wpdb->mb_relationships WHERE from = %d AND type = 'bunkies_to_assembly_steps'", $bunkie_id ) );
    

    But I’m facing inconsistency in the columns displayed, I’ll try to explain myself.

    These are the columns I have when I list my assembly steps:
    Title – Bunkie (which is empty)

    Now when I select a bunkie name from the dropdown and apply the filter:
    Title – Image (custom field created in metabox and displayed via ‘admin_columns’ => true)
    Note that the “Bunkie” column disappeared.

    When I use the “search” field and type a bunkie’s name, it shows me the associated assembly steps with the following columns:
    Title – Bunkie (with proper content)
    Note that the “image” column is absent.

    I could show you screenshots but don’t know how to attach them here.

    Thanks for your help.

    #9040
    Anh Tran
    Keymaster

    Hi, I’m not really clear about the situation. Can you please take a screenshot, upload it on imgur.com and paste the link here?

    #9333
    cedriccouret
    Participant

    Sorry for this long delay,
    I finally ended up not using this so the problem vanished 🙂

    Thanks for your support.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘MB Admin Columns with MB Relationships’ is closed to new replies.

Newsletter

Subscribe to our newsletter to receive news and tutorials for Meta Box and WordPress.


grid gear info copy caret-up caret-down trash