Ben Lobaugh Online

I think, therefore I am. I am, therefore I sail

Author: Ben Lobaugh (blobaugh) (Page 1 of 136)

The brown crusty joys of boat ownership

Cruisers have a saying, “Sailboat cruising is simply boat repair in exotic places.” That may be slightly tongue-in-cheek but boy what I wouldn’t give to be in an exotic place right now.

This time last year I was reinstalling Billabong’s motor. Unfortunately the shaft seal failed shortly after and became progressively worse over the summer. To make matters worse, the salt water was spraying all over my freshly rebuilt and painted motor. It ate right through the paint and rusted up the sides of the motor. Now I get to clean it all up….again…..

Run a wp-cli command across the network

Need to run a wp-cli command across a network of WordPress sites?

Here is an example of how to add/update an option on every site on the network using wp-cli.

wp site list --field=url | xargs -n1 -I % wp --url=% option update my_option my_value

This is really two commands in one. Lets break them down:

wp site list --field=url

This command will get a list of all the sites on the network. The output is a list of the urls for the given site. Passing the url to a command on a network chooses the site to run the command on.

xargs -n1 -I % wp --url=% option update my_option my_value

Here is the kicker. This command takes the output of the previous command into xargs. Xargs then run the new wp command with each site’s url in it.

Using wget to crawl your website

If you are looking to crawl your website for something like cache warming you can do so with wget quite easily.

The following wget command will crawl a site and leave nothing behind on the local filesystem afterward.

wget --mirror -q -e robots=off -p -r --delete-after -nd
–mirror crawls the entire site
-q prevents wget from writing output to the buffer
-e robots=off ignores directives in robots.txt
-p get all page assets
-r recursively request pages
–delete-after clean up any local files after running wget
-nd prevents wget from writing a directory structure that gets left behind

New Toy: 2003 Jeep Grand Cherokee

Find and update WordPress posts with comments older than NNN days

I have a use case where I need to disable comments on posts in WordPress where the last comment was more than one year, or 365 days, ago.

Posts can be found with this query:

SELECT p.ID, p.post_title, p.post_date, p.comment_status, p.`ping_status`
FROM wp_posts AS p
     SELECT c.comment_post_ID
     FROM wp_comments AS c
     WHERE DATEDIFF( curdate(), c.comment_date ) <= 365

The outer query takes the inner query as the search parameter. The inner query is finding the latest comment for each post based on the number in the WHERE condition. 365 represents 365 days, or 1 year.

Therefore, this query is getting the latest comment for post where the comment is less than 1 year old.

Now let’s say you need to update the posts to disable comments. The goal is to disable commenting for all posts that have not had a comment within the last year. It can be accomplished with the following:

UPDATE wp_posts
SET comment_status='closed', ping_status='closed'
		SELECT c.comment_post_ID
		FROM wp_comments AS c
		WHERE DATEDIFF( curdate(), c.comment_date ) >= 365

Here again the number 365 represents the number of days.

Check the posts and you should see the comments have been closed.

Page 1 of 136

Powered by WordPress & Drip City Coffee