Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.