# Fix published date and author for topics created via wp-discourse-embed plugin

**URL:** https://meta.discourse.org/t/fix-published-date-and-author-for-topics-created-via-wp-discourse-embed-plugin/49681
**Category:** WordPress
**Tags:** wordpress
**Created:** [3. September 2016 um 18:48 UTC](https://meta.discourse.org/t/fix-published-date-and-author-for-topics-created-via-wp-discourse-embed-plugin/49681 "2016-09-03T18:48:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [3. September 2016 um 18:48 UTC](https://meta.discourse.org/t/fix-published-date-and-author-for-topics-created-via-wp-discourse-embed-plugin/49681/1 "2016-09-03T18:48:42Z")

</div>

I recently installed [`wp-discourse-embed`](https://github.com/techAPJ/wp-discourse-embed) plugin on [Quarter to Three](http://www.quartertothree.com/fp) WordPress blog.

As soon as I installed the plugin there was an influx of new topics being created for existing (old) WP posts.

I ran this rake task to fix the published date and author for topics that are linked to old WP posts:

```plaintext
desc "Fetch WP data"
task "users:fetch_wp_data" => [:environment] do

  puts "Fetching WP data"
  i = 0
  TopicEmbed.find_each do |topic_embed|
    begin
      html = open(topic_embed.embed_url).read
      raw_doc = Nokogiri::HTML(html)
    rescue
      next
    end

    topic = topic_embed.topic
    next if topic.nil?

    author_username = raw_doc.at('meta[@name="author"]')
    if author_username.present?
      begin
        author = User.where(username_lower: author_username[:content].strip).first
        if author.present? && author.username_lower != topic.user.username_lower
          PostOwnerChanger.new(post_ids: [topic.first_post.id], topic_id: topic.id, new_owner: User.find_by(username_lower: author.username_lower), acting_user: User.find_by(username_lower: "system"), skip_revision: true).change_owner!
        end
      rescue
        # handle error
      end
    end

    wp_post_date = raw_doc.at('meta[@property="article:published_time"]')
    if wp_post_date.present?
      begin
        timestamp = DateTime.parse(wp_post_date[:content].strip).to_i
        PostTimestampChanger.new(topic_id: topic.id, timestamp: timestamp).change!
      rescue ArgumentError
       # handle invalid date
      end
    end

    putc "."
    i += 1
  end

  puts "", "#{i} posts fetched and corrected!", ""
end

```

Commands I ran:

```plaintext
cd /var/discourse
./launcher enter app
vim lib/tasks/users.rake
(append the above rake task to `lib/tasks/users.rake` file)
rake users:fetch_wp_data
(remove the appended rake task from `lib/tasks/users.rake` file)

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [4. September 2016 um 17:13 UTC](https://meta.discourse.org/t/fix-published-date-and-author-for-topics-created-via-wp-discourse-embed-plugin/49681/2 "2016-09-04T17:13:12Z")

</div>

This helps me to figure out some more about how rake works and seeing `PostTimestampChanger` is helpful because it’s taking me a long while to figure out those internals.

My question started out as “why not just add this rake tool to Discourse?”

My answer is that `users:fetch_wp_data` isn’t included in `lib/task/users.rake` because it depends on an external plugin. And then I wondered why bother to delete it, but I think that’s because if we don’t remove it when we’re done, we’ll get into git problems when the next pull happens?

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [4. September 2016 um 17:39 UTC](https://meta.discourse.org/t/fix-published-date-and-author-for-topics-created-via-wp-discourse-embed-plugin/49681/3 "2016-09-04T17:39:38Z")

</div>

> [@pfaffman](#):
>
> My question started out as “why not just add this rake tool to Discourse?”
> 
> My answer is that users:fetch\_wp\_data isn’t included in lib/task/users.rake because it depends on an external plugin. And then I wondered why bother to delete it, but I think that’s because if we don’t remove it when we’re done, we’ll get into git problems when the next pull happens?

Yep, your answer is correct! 💯

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [16. Dezember 2017 um 23:52 UTC](https://meta.discourse.org/t/fix-published-date-and-author-for-topics-created-via-wp-discourse-embed-plugin/49681/4 "2017-12-16T23:52:17Z")

</div>

Hi, I tried your script but removed this part:

```
author_username = raw_doc.at('meta[@name="author"]')
if author_username.present?
  begin
    author = User.where(username_lower: author_username[:content].strip).first
    if author.present? && author.username_lower != topic.user.username_lower
      PostOwnerChanger.new(post_ids: [topic.first_post.id], topic_id: topic.id, new_owner: User.find_by(username_lower: author.username_lower), acting_user: User.find_by(username_lower: "system"), skip_revision: true).change_owner!
    end
  rescue
    # handle error
  end
end

```

Because I only wanted to update the Discourse posts dates.  
The script told me: `61 posts fetched and corrected!` but the dates weren’t changed.

Any idea how to make this work?
