Skip to content

Commit

Permalink
Optimize how the blog tagging runs
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Abraham <[email protected]>
  • Loading branch information
cjyabraham committed Oct 4, 2024
1 parent 9b771e2 commit eb0d280
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,9 @@ public function validate( $input ) {
* Get all projects found in the content.
*
* @param string $content The content of the post.
* @param array $projects The projects to search for.
*/
private function get_project_tags( $content ) {
$projects = get_terms( 'lf-project' );

private function get_project_tags( $content, $projects ) {
$project_tags = array();
foreach ( $projects as $project ) {
if ( strpos( $content, $project->name ) !== false ) {
Expand All @@ -282,11 +281,17 @@ private function tag_blog_posts_with_projects() {
'category' => 230,
)
);
$projects = get_terms( 'lf-project' );

foreach ( $myposts as $post ) {
$projects = $this->get_project_tags( $post->post_content );
if ( get_the_terms( $post->ID, 'lf-project' ) ) {
continue;
}

// only add projects if there are none already assigned.
$project_tags = $this->get_project_tags( $post->post_content, $projects );
if ( ! empty( $projects ) ) {
wp_set_post_terms( $post->ID, $projects, 'lf-project' );
wp_set_post_terms( $post->ID, $project_tags, 'lf-project' );
}
}
wp_reset_postdata();
Expand Down

0 comments on commit eb0d280

Please sign in to comment.