Last active
March 3, 2019 03:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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