User query for timestamp in meta array whne less than today's date

Support MB User Meta User query for timestamp in meta array whne less than today's dateResolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #26558
    rubbergumballrubbergumball
    Participant

    I've given access to content my site by adding meta values to user data using the MB User Meta. The data I need to access is a timestamp, one of two items in an array for that user (the other is the product) and they can have multiple products with different timestamps. Timestamps are used to determine when access is removed.

    How would I query and return the timestamp for all users if timestamp is less than today? I would like to remove access if the timestamp is less than today.

    this is an example of the data stored in Database for the meta field "product_data":
    a:2:{i:0;a:2:{s:14:"product_access";s:3:"prd2";s:15:"acc_expiry_date";i:1618779456;}i:1;a:2:{s:14:"product_access";s:3:"prd2";s:15:"acc_expiry_date";i:1618779456;}}

    this is my current code to return the users but it doesn't return any results:

    <?php
    $date = new DateTime();
    $theDate = $date->getTimestamp();
    
    $user_query = new WP_User_Query( 
        array( 
            'product_data' => 'acc_expiry_date', 
            'meta_value' => '$theDate', 
            'meta_compare' => '<' 
            ) 
        );
            
    // The User Query
    $user_query = new WP_User_Query( $args );
    
    // The User Loop
    if ( ! empty( $user_query->results ) ) {
        foreach ( $user_query->results as $user ) {
            // change meta go here after success test
            $user_info = get_userdata($user->ID);
            echo '<li>' . $user_info->first_name . ' ' . $user_info->last_name . '</li>';
        }
    } else {
        // no users found
    }
    ?>

    Thanks!
    Scott

    #26565
    Long NguyenLong Nguyen
    Moderator

    Hi Scott,

    Follow the class WP_User_Query documentation, to query users by custom field, the key product_data should be meta_key. And you do not need to wrap the variable $theDate in the single quote.

    Get more details on this documentation https://developer.wordpress.org/reference/classes/wp_user_query/#custom-field-parameters.

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