Skip to content

Instantly share code, notes, and snippets.

@verygoodplugins
Created February 19, 2019 19:42
Show Gist options
  • Save verygoodplugins/29e917d94ca9f3463eba0b42dd3d17d6 to your computer and use it in GitHub Desktop.
Save verygoodplugins/29e917d94ca9f3463eba0b42dd3d17d6 to your computer and use it in GitHub Desktop.
Applies tags when a post is viewed based on the names of the categories and post tags assigned to the post
<?php
function wpf_apply_tags_on_view( $post ) {
if( ! function_exists( 'wp_fusion' ) ) {
return;
}
if ( is_admin() || ! is_singular() || ! is_user_logged_in() || $post->post_type != 'post' ) {
return;
}
$tags_to_apply = array();
$categories = get_the_terms( $post, 'category' );
if( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
foreach( $categories as $category ) {
$tags_to_apply[] = $category->name;
}
}
$tags = get_the_terms( $post, 'post_tag' );
if( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
foreach( $tags as $tag ) {
$tags_to_apply[] = $tag->name;
}
}
wp_fusion()->user->apply_tags( $apply_tags );
}
add_action( 'the_post', 'wpf_apply_tags_on_view' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment