$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>disallow publish posts with special title|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

disallow publish posts with special title

matteradmin9PV0评论

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?

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?

Share Improve this question edited Feb 12, 2019 at 23:12 rudtek 6,4035 gold badges30 silver badges52 bronze badges asked Feb 12, 2019 at 22:54 mgt1234mgt1234 1 1
  • Can you provide some context? What problem is this solving for you? – Tom J Nowell Commented Feb 12, 2019 at 23:49
Add a comment  | 

1 Answer 1

Reset to default 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far