Custom Fields
Re-Envisioned
Support MB Custom Table Filterin select advanced with custom field in custom table

This topic contains 4 replies, has 2 voices, and was last updated by  proyectohappyweb@gmail.com 2 weeks, 5 days ago.

  • Creator
    Topic
  • #14286
    Resolved
    proyectohappyweb@gmail.com
    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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #14324

    Anh 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

    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 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

    Perfect!! Many thanks!!

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.