On our site we have a static home page that uses a page template and needs to have a feature image set for it. Setting this up manually is tedious when you multiply by five instances (and have several other manual things to do).
The home page is fully set up in our demo instance, so that gave me a template to work from.
To start, I used a mySQL query to get the row in the
posts table for the home page post in the demo instance. I use this to create a mySQL
INSERT statement so when run, it will insert the post into another instance. I stored this in a file.
I also looked at the postmeta for the home page – that’s where the page template is set. I added another
INSERT statement to my file to add the template to postmeta. I used the mySQL function
LAST_INSERT_INTO() to get the ID of the new post created by the first
INSERT, since that is needed to associate the template with the post.
INSERT INTO lisa_mysite_posts ( post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count) VALUES ( 1, '2015-06-09 20:03:43', '2015-06-09 20:03:43', 'Home Page Content here', 'MySite', '', 'publish', 'closed', 'closed', '', 'home', '', '', '2015-06-09 16:24:10', '2015-06-09 20:24:10', '', 0, 'http://stage.mysite.sas.com/?page_id=1518', 0, 'page', '', 0); INSERT INTO lisa_mysite_postmeta (post_id, meta_key, meta_value) VALUES (LAST_INSERT_ID(), '_wp_page_template', 'page-templates/page-hero.php');
The WP-CLI command for this is simple – just run a db query command that runs the file.
# create page called mySite # slug 'home' # uses template 'page with large hero image' wp db query --path=/mysite_lisa/wp/ --url=lisa.mysite.sas.com < /mysite_lisa/deploy/insert_homepage.sql
Next it needs a feature image. I was able to cheat a bit this time; the image already exists on most of our instances and has the same name, so it’s easy to get the ID by using the name.
I don’t know how the Austin WordPress Meetup folks knew that WP-CLI wants the
post_name field to be referred to as
pagename, but mad props to those folks for helping me out today.
This wp-cli command uses two nested queries. One to get the ID of the new home page, and one to get the ID of the image.
# set featured image wp post meta set \ $(wp post list --pagename=home --field=ID --path=/mysite_lisa/wp/ --url=lisa.mysite.sas.com) \ _thumbnail_id \ $(wp post list --pagename=hero-image-drg --field=ID --path=/mysite_lisa/wp/ --url=lisa.mysite.sas.com)
Finally, I need to set options for the site, so there is a static home page, and that page is our newly created page. I took a look at the options table in mySQL to see what I needed to set, and made a couple of intelligent guesses that seemed to be correct.
# set as Front page wp option update show_on_front page wp option update page_on_front \ $(wp post list \ --pagename=home \ --field=ID \ --path=/mysite_lisa/wp/ \ --url=lisa.mysite.sas.com)