0

Using the S2member plugin, a meta_key for the last-payment-time is automatically stored in WordPress DB under table wp_usermeta (custom field is called wp_meds2member_last_payment_time).

From wp_usermeta, the column called meta_key has a value 'wp_meds2member_last_payment_time'. But the content I'm trying to collect is its meta_value equivalent. The meta_value

I didn't find how to call this meta_value to the function below where I use the date of the last payment time to start a new count of the posts created by the user.

function check_post_limit(){
   if(current_user_can('edit_posts')) {
   global $userdata;
   global $post_type;
   global $wpdb;
   $postintervall = date('Y-m-d H:i:s', $s2member_last_payment_time );
   $item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = $userdata->ID AND post_date > '$postintervall'" );
    if(( $item_count >= 2 ) && (current_user_is("s2member_level1")) )
  { wp_die( 'Do not create too much posts.','check-limit' ); }

return;   }
}

Any suggestions? Thanks

  • Have you tried the get_user_meta function like $s2member_last_payment_time = get_user_meta( get_current_user_id(), 'wp_meds2member_last_payment_time', true ); ? codex.wordpress.org/Function_Reference/get_user_meta – czerspalace Sep 3 '15 at 0:08
  • yes, already tried, it doesn't work. – Amine Richer Sep 3 '15 at 0:18
  • what did your code look like when you called get_user_meta – czerspalace Sep 3 '15 at 0:31
  • function check_post_limit(){ if(current_user_can('edit_posts')) { global $userdata; global $post_type; global $wpdb; $s2member_last_payment_time = get_user_meta( get_current_user_id(), 'wp_meds2member_last_payment_time', true ); $postintervall = date('Y-m-d H:i:s', $s2member_last_payment_time ); $item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = $userdata->ID AND post_date > '$postintervall'" ); if(( $item_count >= 2 ) && (current_user_is("s2member_level1")) ) { wp_die( 'Too much posts.', ); }return;}} – Amine Richer Sep 3 '15 at 0:58
  • 1
    @czerspalace Thanks man, the echo is working fine. That lead me to discover the problem: I double checked the post_type='course' at the beginning of the function if( (current_user_can('edit_posts')) && ( $post_type === 'course' )) and at the count of the posts (courses) item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = $userdata->ID AND post_type = 'course' AND post_date > '$postintervall ' " );. When I deleted the check in the condition of the function it works. Thank you !! – Amine Richer Sep 3 '15 at 14:54
1

@Cristián Lávaque Thank you for the hints ;)

Actually I managed to resolve the issue blocking this function (show the hidden comment above). In case this would help anyone here is the final working code:

function check_post_limit() {
    if ( current_user_can( 'edit_posts' ) ) {
        global $userdata;
        global $wpdb;
        $s2member_last_payment_time = get_user_meta( get_current_user_id(), 
            'wp_s2member_last_payment_time', true );
        $postintervall = date( 'Y-m-d H:i:s', $s2member_last_payment_time );
        $item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts 
            WHERE post_status = 'publish' AND post_author = $userdata->ID 
            AND post_date > '$postintervall'" );
        if ( ( $item_count >= 2 ) && ( current_user_is( "s2member_level1" ) ) ) { 
            wp_die( 'Do not create too much posts.', 'check-limit' ); 
        }
        return;   
    }
}
0

@Amine, the meta field's name is just s2member_last_payment_time, so try it without the wp_med prefix and see if that works.

$2member_last_payment_time = get_user_meta(get_current_user_id(), 's2member_last_payment_time', true);

You can also try s2Member's function get_user_field: http://www.s2member.com/codex/stable/s2member/api_functions/package-functions/#src_doc_get_user_field%28%29

$s2member_last_payment_time = get_user_field ('s2member_last_payment_time');

If no payment actually happened, then no payment time would be recorded, but if there is it'll be a timestamp.

I hope that helps. :)

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.