Publish a WordPress Draft if it’s title contains a specific phrase

I want to write a wordpress plugin that will publish a draft only when the draft’s title contains a certain keyword that I specify. How to achieve that?

Related posts

Leave a Reply

2 comments

  1. function filter_post( $data , $postarr )
    {
      if($data['post_title'] == 'something')
          $data['post_status'] = 'draft';
    
      return ( $data )
    }
    
    add_filter ( 'wp_insert_post_data' , 'filter_post' , 99 );
    

    Should work but didn’t test it.

  2. function filter_post( $data , $postarr ) {
    
        if (!preg_match("/bsomethingb/i", $data['post_title']))
            $data['post_status'] = 'draft';
    
        return ( $data );
    }
    
    add_filter ( 'wp_insert_post_data' , 'filter_post' , '99', 2 );
    

    If the word something doesn’t appear in the tittle text, it becomes a draft, else, it publishes normally. Just change the keyword and it should work.