-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-wordpress-posts.rb
109 lines (91 loc) · 3.22 KB
/
import-wordpress-posts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'rubypress'
require 'nokogiri'
require 'date'
# Import the relevant environment
require File.expand_path('config/environments/development.rb')
require 'json'
wordpress = Rubypress::Client.new(
host: 'globalaccountingnetwork.net',
username: 'admin',
password: 'admin',
retry_timeouts: true
)
# Import blog posts from old website
if Article.count.zero?
wp_posts = wordpress.getPosts(filter: {
number: 100,
orderby: 'post_id',
post_status: 'publish',
post_type: 'blog'
})
wp_posts.each do |wp_post|
article = Article.create!(
title: wp_post['post_title'],
description: wp_post['post_content'],
created_at: wp_post['post_date_gmt'].to_date,
updated_at: wp_post['post_modified'].to_date,
admin_user: AdminUser.first
)
post_content_html = Nokogiri::HTML(wp_post['post_content'])
if img = post_content_html.xpath('//img').first
article.remote_image_url = img.attr('src')
article.save
end
end
end
# Import Job posts from old website
if Job.count.zero?
# Job posts
wp_jobs = wordpress.getPosts(filter: {
number: 100,
orderby: 'post_id',
post_status: 'publish',
post_type: 'vacancy'
})
wp_jobs.each do |wp_post|
job = Job.new(
title: wp_post['post_title'],
created_at: wp_post['post_date_gmt'].to_date,
updated_at: wp_post['post_modified'].to_date
)
intro_text = ''
description = wp_post['post_content']
wp_post['custom_fields'].each do |field|
if field['key'] == 'sector'
job.sector = Job.sectors['Technology, Media & Telecoms'] if field['value'] == 'tech'
job.sector = Job.sectors['Retail, FMCG & Hospitality'] if field['value'] == 'retail'
job.sector = Job.sectors['Private Equity'] if field['value'] == 'equity'
job.sector = Job.sectors['Infrastructure & Prof. Services'] if field['value'] == 'infra'
job.sector = Job.sectors['Property & Facility Management'] if field['value'] == 'property'
job.sector = Job.sectors['Pharmaceuticals & Life Sciences'] if field['value'] == 'pharma'
job.sector = Job.sectors['Mining, Energy & Oil & Gas'] if field['value'] == 'mining'
end
if field['key'] == 'contract_type'
job.job_type = 0
job.job_type = Job.job_types[:permament_full_time] if field['value'] == 'perm'
job.job_type = Job.job_types[:permament_part_time] if field['value'] == 'permpart'
job.job_type = Job.job_types[:fixed_term_full_time] if field['value'] == 'fixed'
job.job_type = Job.job_types[:fixed_term_part_time] if field['value'] == 'fixedpart'
job.job_type = Job.job_types[:contract] if field['value'] == 'con'
end
if field['key'] == 'intro_text'
intro_text = field['value']
end
if field['key'] == 'location'
job.location = field['value']
end
if field['key'] == 'reference'
job.ref_id = field['value']
end
if field['key'] == 'salary_upper'
job.salary = field['value'].to_i
end
if field['key'] == 'salary_upper'
job.salary = field['value'].to_i
end
end
description = "#{intro_text}#{description}"
job.description = description
job.save
end
end