Skip to content

Instantly share code, notes, and snippets.

@artlung
Last active March 3, 2019 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artlung/fed3fe3f72d1fb16350f9963e6a46989 to your computer and use it in GitHub Desktop.
Save artlung/fed3fe3f72d1fb16350f9963e6a46989 to your computer and use it in GitHub Desktop.
Tool for searching through my WordPress blog for posts with hashtags or handles in WordPress titles
<?php
function find_tags($str) {
$tags = [];
$words = explode(" ", $str);
foreach ($words as $word) {
if (substr($word, 0, 1) === "#") {
$value = strtolower(str_replace([".",":","#","!","\"", "@", "(", ")"],"",$word));
if (strlen($value) > 2) {
$tags[] = $value;
}
}
}
return implode(" ", $tags);
}
function find_atsigns($str) {
$tags = [];
$words = explode(" ", $str);
foreach ($words as $word) {
if (substr($word, 0, 1) === "@") {
$value = strtolower(str_replace([".",":","#","!","\"", "@", "(", ")"],"",$word));
if (strlen($value) > 2) {
$tags[] = $value;
}
}
}
return implode(" ", $tags);
}
$lines = file("out.csv");
foreach ($lines as $line) {
$parts = explode(",", $line);
$id = array_shift($parts);
$title = implode($parts, ",");
$tags = find_atsigns($title) . " " . find_tags($title);
if ($tags) {
echo <<<WHAT
wp post term add {$id} post_tag $tags
WHAT;
// echo $title;
echo "<br>";
}
}
# https://developer.wordpress.org/cli/commands/post/
wp post list --post__in=$(wp db query 'SELECT ID FROM artlungblog_posts WHERE \
post_title LIKE "%@%" AND post_status="publish" AND post_type="post"' \
--skip-column-names | paste -s -d ',' - ) --format=csv --fields=ID,post_title > out.csv
# https://developer.wordpress.org/cli/commands/post/
wp post list --post__in=$(wp db query 'SELECT ID FROM artlungblog_posts WHERE \
post_title LIKE "%#%" AND post_status="publish" AND post_type="post"' \
--skip-column-names | paste -s -d ',' - ) --format=csv --fields=ID,post_title > out.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment