0

I want to disallow some titles on wordpress and avoid publishing these posts.

example : "title : last news 8 hours ago"

When this sentence is in the post title, I want disallow publish the post. What's the solution?

1
  • Can you provide some context? What problem is this solving for you?
    – Tom J Nowell
    Feb 12, 2019 at 23:49

1 Answer 1

2

The "easy" answer is: put a filter on it.

add_action( 'transition_post_status', 'my_function', 10, 3 );

function my_function( $new_status, $old_status, $post )
{
    if ( 'publish' !== $new_status or 'publish' === $old_status )
        return;

    if ( 'post' !== $post->post_type )
        return; // restrict the filter to a specific post type

    $title = $post->post_title;

  $restricted_title = "title : last news 8 hours ago";

  if ($title == $restricted_title){ //if title matches unpublish
     wp_update_post(array(
        'ID'    =>  $post->ID,
        'post_status'   =>  'draft'
        ));
  }
}

But if the title is slightly different from the string you hardcode it will fail. My suggestion is to make a list of "restricted words" or phrases and check all of them. Like this:

add_action( 'transition_post_status', 'my_function', 10, 3 );

function my_function($new_status, $old_status, $post){

   if ( 'publish' !== $new_status or 'publish' === $old_status )
        return;

  if ( 'post' !== $post->post_type )
        return; // restrict the filter to a specific post type

  $title = $post->post_title;

  // Add restricted words or phrases separated by a semicolon

  $restricted_words = "word1;word2;word3";

  $restricted_words = explode(";", $restricted_words);
  foreach($restricted_words as $restricted_word){
    if (stristr( $title, $restricted_title)){ //if title matches unpublish
     wp_update_post(array(
        'ID'    =>  $post->ID,
        'post_status'   =>  'draft'
        ));
    }
  }
}

Anyway, my take on this is that you will never be 100% sure that these kind of filter works. You should really do it by hand.

7
  • tnx but... Unfortunately, this code does not work :(
    – mgt1234
    Feb 13, 2019 at 10:13
  • I've tweaked the first solution I posted. What kind of problem do you encounter? Feb 13, 2019 at 10:20
  • I used the same title in the post, and post published There should be a draft it's true?
    – mgt1234
    Feb 13, 2019 at 11:09
  • the code should unpublish the post and put it in draft status. check you've transcribed correctly the third if condition (if ($title == $restricted_title){ ) Feb 13, 2019 at 11:11
  • sorry, I've tested it now and worked, but Only this sentence is supported, example : "title : last news 8 hours ago:new musics at weekend..." not detected ! How to use this code for any sentences using restricted_title? and how to use for custom post type?
    – mgt1234
    Feb 13, 2019 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.