# 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:** [03.Сентябрь.2016 18:48:42 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:** 1\
**Showing post:** 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:** [03.Сентябрь.2016 18:48:42 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)

```

---

_[View the full topic](https://meta.discourse.org/t/fix-published-date-and-author-for-topics-created-via-wp-discourse-embed-plugin/49681)._
