Editing post date?

Hi there,

How would I go about altering the post date on a topic or post within a topic?

1 Like

There is no way to do this at the current timeā€¦ no pun intended.

2 Likes

Poo :frowning:

Does ā€˜not possibleā€™ also include from the command-line?

Itā€™s probably possible via Ruby command line or direct SQL, of course. I only meant the web UI.

Hereā€™s how to do it via the command line

# ssh into your server
cd /var/discourse # or /var/docker on pre-V1 install
./launcher enter app
rails c
post = Post.find_by(id: 1234)
post.created_at = DateTime.new(2001,2,3,0,0,42) # year, month, day, hour, minute, seconds 
post.save
3 Likes

Awesome! This seems to have worked, but Iā€™m unclear on what Iā€™m supposed to do or how to safely exit once Iā€™ve altered the date/time.

Looking at my test install, the date for that post hasnā€™t changedā€¦ Iā€™m sure Iā€™m missing something? Thanks for your help!

CTRL + D is how you exit :wink:

2 Likes

Or by typing exit or !!!. :grin:

2 Likes

Hmā€¦ I manage to exit out. I was doing it properly (but wanted to ask in case I was just being a moron), but get a message saying:

Unable to write to your history file, history not saved

Thanks again for your help and patience guys; Ruby is still pretty new to me but Iā€™m learning to love it.

Yeah, thatā€™s a different thing all together, one that you probably donā€™t need to worry about. It is simply telling you it didnā€™t save the commands you entered so when you connect later you could use history to see what commands you ran previously.

If you re-run rails c and type history, you should get a blank list. If you enter a few commands and run it again, you will see a list of the commands you have run up to that point.

1 Like

Is there something else I need to do to get the changes to show up on the front-end then? The post I edited is still showing the original date. Reboot Discourse, perhaps?

Iā€™m not sure, @zogstrip does he need to rebake the post? Or clear any caches?

1 Like

@zogstrip @cpradio

Disregard my last questionā€¦ In looking at the output in red in those screenshots above, I donā€™t seem to be targeting the post I think I amā€¦ The summary text in there is coming from one of the posts that get created when making a new category.

How would I properly determine a postā€™s ID? This one, for example:
http://forum.curiouscosmos.com/t/object-floating-above-newfoundland-iceberg/37/2?u=cosmo

Performing a view source, contains this:
<article class="boxed" id="post_2" data-bindattr-28="28" data-post-id="39" data-bindattr-29="29" data-user-id="1">

Which has data-post-id="39"

I canā€™t promise that is the correct answer thoughā€¦

1 Like

Yessir, that was it! I suppose it helps if Iā€™m actually changing the correct post ID :wink:

Thank you for all your help!

:beers:

1 Like

By the way, you can also find the post by the topic id and post number which are perhaps easier to read from the forum via the share link instead of inspecting source:

Post.find_by(topic_id: 123, post_number: 45)

3 Likes

This is working excellently, though I do have one more question:

How do I force a refresh or update of the last ā€œactivityā€ on the thread listing page? If all of the dates within a thread have a date of May or June, the last activity shouldnā€™t say 22 minutes ago

Thanks guys :slight_smile:

You can see how Discourse does this internally in the PostCreator class:

https://github.com/discourse/discourse/blob/v1.0.0/lib/post_creator.rb#L195-L203

So in your case, this should do:

t = Topic.find(123)
t.last_posted_at = t.posts.last.created_at
t.bumped_at = t.posts.last.created_at
t.save!
3 Likes

Thanks! That pointed me in the right direction.

I found that I had to also update bumped_at and updated_at (or whatever their proper names are, Iā€™m not looking at the screen right now).

2 Likes

The bumped_at field is what is displayed & sorted by.

1 Like