Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Conditional: current user updated post in the last X days WordPress

  • SOLVED

I'm looking for a conditional whereby it checks to see if the current user has updated their custom post (CUSTOMPOST) within that last X days (or hours). If they have updated do this, if they haven't updated do that, else do whatever.

I currently have some other conditionals as well that I'd like the above integrated into...

<?php if (( 0 == count_user_posts( get_current_user_id(), "CUSTOMPOST" ) && is_user_logged_in() && current_user_can('school-cap')) && !current_user_can('admin-cap') ) :?>

Answers (2)

2016-09-09

Rempty answers:

Maybe you need something like this:


global $wpdb;
$pre=$wpdb->prefix;
$date=date("Y-m-d H:i:s",strtotime("-1 days"));
$userid=get_current_user_id();
$sql="SELECT p.ID FROM ".$pre."posts as p WHERE p.post_modified >= '".$date."' AND p.post_author='".$userid."' AND p.post_type='CUSTOMPOST'";
$modpost=$wpdb->query($sql);
if($modpost){
//Do what you need to do
}

I am doing a custom query to get the posts modified in the last 1 day, i think is easy to understand.


Rempty comments:

By default wordpress add to cache the sql querys.
The SQL query provided is secure because you can't inject SQL, don't use $_POST or $_GET.
And custom sql querys are used in a lot of plugins.
Don't have any risk if you prepare it good.


pjeaje comments:

<blockquote>Don't have any risk if you <strong>prepare it good</strong></blockquote>
How does a nube like me do that?


pjeaje comments:

@Rempty: How would I incorporate my original conditionals into your code?


Rempty comments:


global $wpdb;
$pre=$wpdb->prefix;
$date=date("Y-m-d H:i:s",strtotime("-1 days"));
$userid=get_current_user_id();
$sql="SELECT p.ID FROM ".$pre."posts as p WHERE p.post_modified >= '".$date."' AND p.post_author='".$userid."' AND p.post_type='CUSTOMPOST'";
$modpost=$wpdb->query($sql);

<?php if (( 0 == count_user_posts( get_current_user_id(), "CUSTOMPOST" ) && is_user_logged_in() && current_user_can('school-cap')) && !current_user_can('admin-cap') && $modpost) :?>

2016-09-09

dimadin answers:

If your conditional is within loop, it should go like this:


if ( get_the_modified_time( 'U' ) > ( time() - HOUR_IN_SECONDS ) ) {
// updated
} else {
// didn't update
}


Just change HOUR_IN_SECONDS to appropitate number of second when post should have updated.

If it's not within loop, pass object of post you want to check as a second parameter of get_the_modified_time(), so it should look like get_the_modified_time( 'U', $post )


pjeaje comments:

Can you show me the full example with the outside loop please.


pjeaje comments:

Also I need this modified time to apply to the current logged in user and of a specific post-type.


pjeaje comments:

So what I mean is... a user logs in and they visit a specific page. On this page a conditional checks if the current user has updated their custom post.

this code does not apply to the current post, it applies to the current user.


dimadin comments:

Examle outside loop, where $post is post you want to check:


if ( get_the_modified_time( 'U', $post ) > ( time() - HOUR_IN_SECONDS ) ) {

// updated

} else {

// didn't update

}


But this examples are for all authors, not just currently logged in user. If post can be authored by multiple users, this won't work, but if post you want to check is authored only by single user, this works.

So you just need to know $post (that can be even custom post ID, not post object).


pjeaje comments:

I don't think you understand what I'm asking... please re-read the question. This pertains to the current user, not the current post.


dimadin comments:

This doesn't have to be about current post, I showed you example that works for ANY post, on whatever place you want. You just need to know what is user's post. That data you need to know no matter what code you get, because how could you know when is post last updated when you don't know what post to check?


pjeaje comments:

I don't want it about any post, I want it about the current user.


pjeaje comments:

If you can show me how/what you mean and how it relates to my other code then that would be great.


pjeaje comments:

I need the entire completed code as per my question specs please.


dimadin comments:

I can't see what you other code is so I can't do that. Example you showed above is not related. My last code example should work for you, I explained how. If you don't understand this, than sorry, I can't do anything more with stuff you provided.


pjeaje comments:

If you can see my question you can see my code...
<?php if (( 0 == count_user_posts( get_current_user_id(), "CUSTOMPOST" ) && is_user_logged_in() && current_user_can('school-cap')) && !current_user_can('admin-cap') ) :?>


dimadin comments:

That code is unrelated, it checks if user has no post of 'CUSTOMPOST' type, that is logged in, that has 'school-cap' capability while it doesn't have 'admin-cap' capability. Like I said, this is not related to your question.


pjeaje comments:

The code is related to my question, I want the example conditional to also take into account when last the current user updated their CUSTOMPOST post-type.


dimadin comments:

@Rempty: You shouldn't do direct database queries when you have native WordPress function, because there are things like permissions, caching etc.


pjeaje comments:

I agree, I would prefer a non-sql query solution :)


dimadin comments:

@pjeaje: It it not related because it doesn't show a way how you retrieve post of current user, what is a connection between user and post etc.


pjeaje comments:

I don't see what your confusion is in what I require? In any case I don't think you can help me any further. Thanks for trying.