I need a function that does the following when a new post is published:

  1. Checks all existing posts and removes the tag "latest" if it exists
  2. It adds the tag "latest" the the post being published

This way only the post that was most recently published will have the tag "latest"

So far I have been able to add the tag when a post is published:

function send_new_post($new_status, $old_status, $post) {
  if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
    $post = get_post();
    $id = $post->ID;
        wp_set_post_tags( $id , 'latest', true );             
  • Why do you need this tag? There's much simpler ways to get the last published post than this. – Jacob Peattie Jan 25 '19 at 10:52
  • I am using multiple loops that are called as remplate includes... so the end goal is to hide the post if it is the latest post, keep in mind that the post may not be in all categories, so adding "offset=>1" will not work – Sony ThePony Jan 25 '19 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.