Skip to content

Commit

Permalink
Temporary function to populate project taxonomy for blog posts
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 df481ec commit 9b771e2
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ public function display_plugin_setup_page() {
*/
public function validate( $input ) {

$this->tag_blog_posts_with_projects();

$options = get_option( $this->plugin_name );

$options['show_hello_bar'] = ( isset( $input['show_hello_bar'] ) && ! empty( $input['show_hello_bar'] ) ) ? 1 : 0;
Expand Down Expand Up @@ -251,6 +253,45 @@ public function validate( $input ) {
return $options;
}

/**
* Get all projects found in the content.
*
* @param string $content The content of the post.
*/
private function get_project_tags( $content ) {
$projects = get_terms( 'lf-project' );

$project_tags = array();
foreach ( $projects as $project ) {
if ( strpos( $content, $project->name ) !== false ) {
$project_tags[] = $project->name;
}
}
return $project_tags;
}

/**
* Tag blog posts with projects.
* This is a temporary function used once to tag all blog posts by projects that appear in its copy.
*/
private function tag_blog_posts_with_projects() {
$myposts = get_posts(
array(
'post_type' => 'post',
'posts_per_page' => -1,
'category' => 230,
)
);

foreach ( $myposts as $post ) {
$projects = $this->get_project_tags( $post->post_content );
if ( ! empty( $projects ) ) {
wp_set_post_terms( $post->ID, $projects, 'lf-project' );
}
}
wp_reset_postdata();
}

/**
* Update options
*
Expand Down

0 comments on commit 9b771e2

Please sign in to comment.