Relationships search functionality

Support MB Relationships Relationships search functionality

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #44450
    EliodataEliodata
    Participant

    Hello, I'm using the MB Relationships extension to link a custom post type with WordPress users. The metabox on the cpt edit page allows selecting users through a dropdown, which currently searches by first and last names. However, I'd like to enhance this search functionality to include the 'billing_company' field as well, so the dropdown displays 'billing_company first_name last_name' instead of just 'first_name last_name'. How can I achieve this? The ID of the MB Relationship field is 'clients-wp-bdd'

    regards

    #44454
    PeterPeter
    Moderator

    Hello,

    Currently, the relationship extension doesn't support an option or a filter hook to adjust the search result and display items. I will inform the development team to explore the possibility.

    Thank you.

    #44455
    EliodataEliodata
    Participant

    Thank you for the quick answer. Hope they will find something.
    If you give some clues maybe I could on my side.
    Best regards

    #46919
    Markus MillendorferMarkus Millendorfer
    Participant

    Hi, is there a plan to add this functionality.
    I have a post type with 500 entries (business location) - some of them with similar names but different address - so the relation field can not be used by us, as you never know which item gets selected in such cases.

    #46922
    EliodataEliodata
    Participant

    Hi,

    I finally use this snippet to display a metabox in my cpt with a link to the related user. The link display the billing company field, but you can use any other user field.

    // MODIFIER AFFICHAGE CHAMP relationnel clients-wp-bdd METABOX.IO cote BDD clients
    add_action( 'add_meta_boxes', 'add_link_to_wp_user_meta_box' );
    
    function add_link_to_wp_user_meta_box() {
        add_meta_box(
            'link_to_wp_user_meta_box',     // ID unique
            'Lien vers le compte utilisateur WP', // Titre de la metabox
            'display_link_to_wp_user_meta_box',  // Fonction callback pour afficher le contenu
            'client',  // Le CPT auquel cette metabox est liée
            'side',    // Position de la metabox
            'high'     // Priorité de l'affichage
        );
    }
    
    function display_link_to_wp_user_meta_box( $post ) {
        global $wpdb;
    
        // Récupérer l'ID de l'utilisateur associé au CPT via la relation 'clients-wp-bdd'
        $client_id = $post->ID;
        $user_id = $wpdb->get_var( $wpdb->prepare(
            "SELECT <code>from</code> FROM {$wpdb->prefix}mb_relationships WHERE <code>to</code> = %d AND <code>type</code> = 'clients-wp-bdd'",
            $client_id
        ));
    
        if ( !empty( $user_id ) ) {
            // Récupérer les informations de l'utilisateur WP
            $user = get_userdata( $user_id );
            $billing_company = get_user_meta( $user_id, 'billing_company', true );
            $edit_link = get_edit_user_link( $user_id );
    
            // Afficher un lien cliquable vers la page d'édition de l'utilisateur WP
            if ( !empty( $billing_company ) ) {
                echo '<p>Société : <a href="' . esc_url( $edit_link ) . '">' . esc_html( $billing_company ) . '</a></p>';
            } else {
                echo '<p>Aucun champ "billing_company" trouvé pour cet utilisateur.</p>';
            }
        } else {
            echo '<p>Aucun utilisateur lié trouvé.</p>';
        }
    }
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.