Question

I run a multiple author Wordpress site with thousands of posts. To highlight good posts, I filter them by specific tags that only admins can set. For instance featured, front page etc.

To avoid my authors selecting these tags themselves, I use the following script. If the user has selected a prohibited tag, it will be removed once they click the publish button. Comments are for convenience:

add_action('save_post', 'remove_tags_function', 10, 1); //whenever a post is saved, run the below function

function remove_tags_function( $post_id ){
    if(!current_user_can('manage_options')){ // if the logged in user cannot manage options (only admin can)
        $post_tags = wp_get_post_terms( $post_id, 'post_tag', array( 'fields'=>'names' ) ); //grab all assigned post tags
        $pos = array_search( 'tag-to-be-deleted', $post_tags ); //check for the prohibited tag
        if( false !== $pos ) { //if found
            unset( $post_tags[$pos] ); //unset the tag
            wp_set_post_terms ($post_id, $post_tags, 'post_tag'); //override the posts tags with all prior tags, excluding the tag we just unset
        }
    }//end if. If the current user CAN manage options, the above lines will be skipped, and the tag will remain
}

There is a major issue with this solution. Once the post has been published, an admin gives it a featured tag - however if the original author would make any updates to their post, the tag will disappear. Do you understand the problem?

Many authors like to do revisions to their posts, especially when receiving feedback in the comments, and then there are the type of posts that are news related, which requires to be updated frequently.

What solution can you propose that will solve this scenario? Admins will need to be able to give the featured tag, and if the author updates their post, the tag should remain. What a puzzle...

Was it helpful?

Solution

I would propose not using tags at all for your admin properties. In essence, all you are looking to do is mark certain posts with certain attributes.

For this I might sugest using a custom taxonomy.

See the linked article, especially the "Using Labels For Taxonomies" section to give you some ideas on how to add custom handling for your attributes, that could be kept separate from the tag system altogether.

The Link

OTHER TIPS

You have to distinguish tags added by user and tags added by admin and process them separately. Add a flag column to tags or keep tags from users and admins in separate tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top