• Resolved futur3int

    (@futur3int)


    Hi all,

    I would like to rule the way the comment area is displayed or not in single posts according to a specific user meta field.

    For instance: When a new post is released > The comment area will show after x days (from the post release date) only to users on custom meta 1 while users on custom meta 2 can see it straight away.

    TY

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You would alter your theme’s comment template with code to accomplish that. Get user meta with get_user_meta(). Clever how they name functions ?? Conditionally execute parts or all of the comment template based on the meta value and the difference between post date and the current date/time. To easily do date differences, the dates should be expressed as UNIX timestamps. time() returns the current timestamp. Get the post timestamp with strtotime(). The difference will be seconds since the post was published. Divide by (60*60*24) to get days.

    Thread Starter futur3int

    (@futur3int)

    Hi BC,

    Using Wp User query the metafield part is working but still im trying to make the time function working. Seems the Math is not taken in account.

    I am using the following:

    $now = time(); // right now
    	$postdate = strtotime(get_the_date()); // the post date
    	$datediff = $now - $postdate; // math 
    	$daysdiff = floor($datediff / (60 * 60 * 24)); // rounding down 
     if($daysdiff >= 4 ){ // if the post is older than 4 days
    	comment_form();
    
    }

    The comment box is displaying no matter what although the post is not older than 4 days.

    Also I just realized that the post is in “Fixing WordPress” instead of “Developing with wordpress” … Maybe you can move it to?

    Thank you.

    • This reply was modified 6 years, 10 months ago by futur3int.
    • This reply was modified 6 years, 10 months ago by futur3int.
    • This reply was modified 6 years, 10 months ago by futur3int.
    Thread Starter futur3int

    (@futur3int)

    Done!

    I did the math using echo on variables. $now = time(); was not giving anything.
    So i used $now = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) ); to get current time. I also did some changed in the code. Im sharing the following working code:

       $now = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) ); // right now
    	$ts1 = strtotime($now);
        $ts2 = strtotime(get_the_date('Y-m-d H:i:s'));
    	$datediff = $ts1 - $ts2;
    	$daydiff = floor($datediff / 86400);
     	if($daydiff >= 4 ){ // if the post is older than 4 days
    	comment_form();
    
    } else {
        echo "Post is not old enuf";
    	}

    If anyone want help on how to setup restrict content with user meta field please feel free to ask.

    Cheers

    • This reply was modified 6 years, 10 months ago by futur3int.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show post comment area in time for user with specific meta field value’ is closed to new replies.