Filterin select advanced with custom field in custom table

Support MB Custom Table Filterin select advanced with custom field in custom tableResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #14286
    proyectohappyweb@gmail.com[email protected]
    Participant

    Hello,

    I want to select_advanced field, but only get the users that meet a condition with respect to a proper field saved in custom table users.

    $meta_boxes[] = [
            //'title'       => '',
        'id'            => 'id_field_metabox',
        'type'          => 'user',
        'storage_type'  => 'custom_table',    // Important
            'table'         => 'usuarios', // Your custom table name
        'fields' => [
            [
            'id'   => 'id_field_metabox_select',
            'name' => '',
            'type' => 'user',
            'multiple'    => true,
            'field_type'  => 'select_advanced',
                    'query_args' => array(
                        'meta_key' => 'custom_field_A',
                        'meta_value'  => '0',
                 ),
                 ],
             ],

    I try this:

                  'query_args' => array(
                        'meta_key' => 'custom_field_A',
                        'meta_value'  => '0',
                 ),

    But not run: "No results found".

    Where custom_field_A was created before:

         [
            'id'   => 'custom_field_A', 
            'name' => 'Custom Field A',
            'type' => 'text',
        'std'  => 0, // 0 or 1',
            ],
    ];

    It's possible this?

    Many thanks,
    Sergio

    #14324
    Anh TranAnh Tran
    Keymaster

    Hi Sergio,

    The meta_key and meta_value in the query_args will tell the plugin to filter users by WordPress custom fields, e.g. user meta. They don't handle the value in the custom table.

    #14336
    proyectohappyweb@gmail.com[email protected]
    Participant

    Oh, thanks, I understand.

    So I do not know if it can be done or if in this forum you could guide me for the following:
    To obtain in the results of the select_advanced the users of a custom table related to the users according to a condition that occurs on a field saved in the custom table itself.

    Thanks

    #14380
    Anh TranAnh Tran
    Keymaster

    Hi Sergio,

    I think that can be done with an extra query to the data base:

    • Make a custom query to the custom table to get the ID of the users you want to select:
    global $wpdb;
    $user_ids = $wpdb->get_col( "SELECT ID FROM yourtable WHERE field_1 = 'value_1'" );
    • Then in your options of the user field, set the include param:
    [
    'type' => 'user',
    'query_args' => [
        'include' => $user_ids, // get from above
    ],
    ]
    #14387
    proyectohappyweb@gmail.com[email protected]
    Participant

    Perfect!! Many thanks!!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.