0

I use Wordpress 4.8 and I want to get users by meta values. I want users not connected since 7 days, and with 'email_not_logged' meta empty.

So I tried :

$users = get_users(array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'meta_key' => 'last_login',
            //'meta_value' => 'DATE_SUB(NOW(),INTERVAL 7 DAY)',
            'meta_value' =>  date('Y-m-d H:i', strtotime('-7 days')),
            'meta_compare' => '<=',
            'type' => 'DATE',
        ),
        array(
            'meta_key' => 'email_not_logged',
            'meta_compare' => 'NOT EXISTS',
        )
    ))
);
global $wpdb;
echo "<pre>";
print_r($wpdb->last_query);
echo "</pre>";

I don't know why but I get :

SELECT user_id, meta_key, meta_value FROM ma_usermeta WHERE user_id IN (82) ORDER BY umeta_id ASC

How can I have the good query to test it ?

But, this works :

$users = get_users(
    array(
        'meta_key' => 'last_login',
        //'meta_value' => 'DATE_SUB(NOW(),INTERVAL 7 DAY)',
        'meta_value' =>  date('Y-m-d H:i:s', strtotime('-7 days')),
        'meta_compare' => '<=',
        'type' => 'DATE',
    )
);

Do I use badly meta_query ?

0

get_users() is a convenience wrapper around WP_User_Query. Maybe you can use that directly, e.g.

$user_search = new WP_User_Query(
    array(
        'meta_query' => array(
            'relation' => 'AND',
            array(
            'meta_key' => 'last_login',
                //'meta_value' => 'DATE_SUB(NOW(),INTERVAL 7 DAY)',
                'meta_value' =>  date('Y-m-d H:i', strtotime('-7 days')),
                'meta_compare' => '<=',
                'type' => 'DATE',
            ),
            array(
                'meta_key' => 'email_not_logged',
                'meta_compare' => 'NOT EXISTS',
            )
        )
    )
);

And then look at the SQL it generates by printing $user_search->request?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.