1

I'm trying to get the users that have registered after a specific time (this case $today) and who have the newsletter field true and the account false. This is what I have so far but the result is always 0.

$new_users_newsletter = $wpdb->get_results(
    "SELECT *
    FROM $wpdb->users
    RIGHT JOIN $wpdb->usermeta
    ON $wpdb->users.ID = $wpdb->usermeta.user_id
    WHERE ($wpdb->users.user_registered >= '$today'
        AND $wpdb->usermeta.meta_key LIKE 'newsletter' AND $wpdb->usermeta.meta_value LIKE 'true'
        AND $wpdb->usermeta.meta_key LIKE 'account' AND $wpdb->usermeta.meta_value LIKE 'false')
    GROUP BY $wpdb->users.ID"
);

1 Answer 1

2

You can try this with WP_User_Query instead:

$args = array (
    'meta_query'    => array(
        array(
            'key'   => 'newsletter',
            'value' => 'true',
        ),
        array(
            'key'   => 'account',
            'value' => 'false',
        ),
    ),
    'date_query'    => array(
        array(  
            'after'     => '2015-01-13 00:00:00',
            'inclusive' => true,
        ),          
     ),
);

$user_query = new WP_User_Query( $args );

where I assume your user meta values are boolean strings, i.e. 'true' and 'false'.

0

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.