File this under āthings you donāt expect to find in the shared office kitchenāā¦
Quoting: Men at Arms (Discworld, #15; City Watch #2)
āThe reason that the rich were so rich, Vimes reasoned, was because they managed to spend less money.
Take boots, for example. He earned thirty-eight dollars a month plus allowances. A really good pair of leather boots cost fifty dollars. But an affordable pair of boots, which were sort of OK for a season or two and then leaked like hell when the cardboard gave out, cost about ten dollars. Those were the kind of boots Vimes always bought, and wore until the soles were so thin that he could tell where he was in Ankh-Morpork on a foggy night by the feel of the cobbles.
But the thing was that good boots lasted for years and years. A man who could afford fifty dollars had a pair of boots thatād still be keeping his feet dry in ten yearsā time, while the poor man who could only afford cheap boots would have spent a hundred dollars on boots in the same time and would still have wet feet.
This was the Captain Samuel Vimes āBootsā theory of socioeconomic unfairness.ā
I had the need to change the status on several hundred wordpress posts by a particular author, from āpublishā to āpendingā (more on this in a future post). This would have taken me hours to do through the frontend, so I figured Iād make my first serious use of WP-CLI, and script the job.
You can list posts using WP-CLI, and specify which columns to display, and even pass in basic filters, like so wp post list --<column>=value --fields=ID,post_title,post_status,post_author
. You can also update posts. By combining these with some shell scripting, big jobs can be done fairly easily.
Easily. Oh, the hubris. The problem I ran into was:
- The display column names are the names of the columns in the database, so to list the post author, you would refer to it as post_author.
- The filter column names are as they are referred to in WP_Query, not how they are named in the database.
I didnāt catch on to this distinction at first, and the number of rows I was returning was large enough it couldnāt display all of them in the terminal. Yes, I should have used less
to check. I should have done a lot of things, like take a backup first (wp db export ~/export.sql
), but that wouldnāt be as good a cautionary tale.
Long story short, I ran wp post list --post_author=3 --fields=ID,post_title,post_status,post_author
, saw only the results I expected because some rows were cut-off. When I fed this into a loop which would update the post_status, I ended up setting every post as pending.
wp post list --post_author=3 --format=ids \
| xargs -d ' ' -I % wp post update % --post_status=pending
The correct command should have been
wp post list --author=3 --format=ids \
| xargs -d ' ' -I % wp post update % --post_status=pending
Thankfully, having realised my mistake, I could make use of WP-CLI to fix it:
wp post list --author=2 --format=ids \
| xargs -d ' ' -I % wp post update % --post_status=publish
For everyone following along at home, my mistakes so-far were:
- Not reading the documentation fully
- Not taking a backup before altering a live site
- Not double-checking my test results before running the command āfor realā
Iāve returned all of the wrongly-pended posts to āpublishā status. I do still have an issue with Post-Kinds data being missing on most of these posts; I think this is due to some wierd interaction between WP-CLI and the plugin, but I canāt be sure. These posts (~60) Iām going to have to fix by hand ā I consider it a reprimand for my earlier flippant approach!
Cross-posted to /en/wordpress.
āRed sky at nightā¦ā
Iāve got no mojo today for the painting Iād planned to do, so Iām spending the day building the next batch of Hobby Goals units, while watching the LCO coverage. Gotta stay productive! 1 down, 5 to go.
Iāve been chipping away at K as time allows over the last week. Itās still a long way from where Iād like it to be, but itās getting thereā¦
- All theme files should be compliant with the WordPress Coding Standards, apart from a few instances where Iām planning to rewrite whatās there.
- Iāve made little tweaks here and there to the markup output by the theme, and plan to revisit this continually. I made a test post to indieweb.xyz, but it didnāt quite parse correctly last time. This post should also be submitted ā fingers crossed it goes better this time! Iām also curious if IndieNews posting will be more successful.
- Files have been refactored (albeit not yet fully reorganised), with a goal of splitting up what was a growing, monolithic functions.php into smaller logical chunks.
- Iām wrestling with how to handle all of the different kinds of posts the theme will support, from a markup and display point of view. My biggest headache at the moment is the treatment of post titles. I might write another post on this topic.
- Iāve been working on making plug-in support more optional than I had it at the outset. I make heavy use of Post Kinds and other IndieWeb plug-ins, but donāt want the theme to necessarily rely on them.
- Oh, and the source code is now available on GitHub, in the spirit of āselfdogfoodingā. I came to the realisation there wasnāt anything to be gained by holding the code back, and I even ran the risk of never releasing anything if I waited until it was āready.ā
Iām learning quite a bit through this exercise, which is great. Itās reinvigorated some of my love of code tinkering; Iām finding that some evenings Iām more keen to sit down at a keyboard than I am to sit at my hobby station, which is something thatās not happened in a long time!
Update 09:26 ā IndieNews submission is still failing, unfortunately. Indieweb.xyz seems to be working, but I had to force a Webmention.
Just had one of the back garden fence panels implode under the high winds, and several others looking like they might go too. Just the thing I needed first thing in the New Year.
Learning about how filthy the new Gloomspite Gitz are.