I havea CRED post form that allows a logged in user to become the new post author. Essentially, the user is "Claiming" the post and becomes the posts author with a specific permissions level.
However, even though CRED posts forms alow you to decide the status of the post, there is not an option to update the post date at the same time.
I have the CRED posts form status set to publish, which is what I want however, I need to update the posts date when the user "Claims" the post and becomes the new post author.
My current code in functions.php look like this:
//assign post author based on url parameter
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==215)
{
$args = array(
'ID' => $post_id,
'post_author' => get_current_user_id(),
);
$res = wp_update_post( $args, true );
}
}
How can I modify this to update the posts date at the same time?
Why not use the Post Edit Time?
That can be displayed with a ShortCode and is added by WordPress automatically just like the Post Date.
However, to apply this you would need to hook a WordPress code that updates the Post Date to a Forms API hook (best is cred_save_data() as you correctly show above.
I see that you already started becoming familiar with the WordPress API you need for this task (it is irrelevant from the Toolset API in this case).
To further enhance that code you need co consult this DOC:
https://codex.wordpress.org/Function_Reference/wp_update_post
It describes the parameters of wp_update_post, where $post will accept any array or object representing the elements that make up a post. There is a one-to-one relationship between these elements and the names of columns in the wp_posts table in the database. You must include "ID", otherwise a WP_Error will be thrown.
So that means you need to add post_date to your array and update that with a valid date.
https://codex.wordpress.org/Database_Description#Table:_wp_posts > post_date
Then, the code will update your post_date as well, hooked to the cred_save_data it will do that when the form saves the post in the database.
This is however not subject to Toolset Support, as it makes no usage of the Toolset API, the focus is on WordPress API as above outlined.
Thanks!
Beda,
Thanks for your assistance. I don't need to display the time on the front end, so I am not sure using Post edit time short code would be necessary.
I just need a way that post date gets updated when the new user "Claims" the post. I appreciate you pointing to me wordpress codex and I am sure that "post_date" sounds like the right direction. However, I am uncertain how to do this properly.
I related this issue to Toolset CRED, because, I am trying to display a descending list of items by post date. But that list is specific to each users account.
So that manner in which toolset does display descending order by post date works properly, but the issue is, I want the list to show that order in which the user "claims" the post and not by the date in which the post was entered into wordpress. The idea, is to have the user's dashboard always show the last post that they claimed first. That is why I realized the post date must be updated when the user "claims" the post.
You pointed me in the right direction, but I am still unsure how to do this properly. I thought that when a user saves the cred form (after claiming the post) that they are essentially re-publishing the post which would update the post date. But, that is not the case. If it were, no additional code would be needed.
Is there any further assistance you might be able to help with?
Hi, you can edit the post_date and post_date_gmt like this:
$time = current_time('mysql');
$args = array(
'ID' => $post_id,
'post_author' => get_current_user_id(),
'post_date' => $time,
'post_date_gmt' => get_gmt_from_date($time),
);
$res = wp_update_post( $args, true );
Christian and Beda,
Thanks so much for your help! Christian, that works perfectly!
Exactly what I wanted 😉
Can't thank you enough. This thread is resolved.
Okay great, feel free to reopen the ticket if you need further assistance.