您必须首先检查现有条款wp_get_object_terms()
如果缺少所需的术语,请将其添加为wp_set_object_terms()
.
只更新一次术语比为每个术语更新更快。
以下示例正在上运行save_post
. 我将所有内容都放入一个类(和插件)中,以使代码更具可读性。您可以只使用一个函数。
为了保持灵活性,我实现了三种匹配算法:精确、不区分大小写和正则表达式(regex)
条款必须已经存在。我选择此要求是为了防止意外的不匹配。
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Term by title
*/
add_action( \'save_post\', array ( \'WPSE_39700_Term_By_Title\', \'init\' ), 10, 2 );
class WPSE_39700_Term_By_Title
{
/**
* Post types to process.
*
* @var array
*/
protected $post_types = array ( \'post\', \'page\' );
/**
* What to search for? Adjust this to your needs.
*
* @var array
*/
protected $searches = array (
\'Dog Training\' => array ( // string to search for
\'search_type\' => \'exact\', // \'exact\', \'case-insensitive\' or \'regex\'
\'taxonomy\' => \'category\', // existing taxonomy
\'term\' => \'Dog Training\' // existing term name
),
\'~\\d\\s*(kg|kilogram)~i\' => array ( // \'43kg\' or \'3 kilogram\'
\'search_type\' => \'regex\',
\'taxonomy\' => \'post_tag\',
\'term\' => \'Health\'
),
\'wordpress\' => array (
\'search_type\' => \'case-insensitive\',
\'taxonomy\' => \'post_tag\',
\'term\' => \'WordPress\'
)
);
/**
* Current post object.
*
* @var object
*/
protected $post = NULL;
/**
* Terms to update.
*
* @var array
*/
protected $update = array ();
/**
* Initial callback function.
*
* @wp-hook save_post
* @param int $post_id
* @param object $post
* @return void
*/
public function init( $post_id, $post )
{
new self( $post_id, $post );
}
/**
* Constructor called by init.
*
* @param int $post_id
* @param object $post
*/
public function __construct( $post_id, $post )
{
// wrong post type
if ( ! in_array( $post->post_type, $this->post_types )
// no title
or \'\' === trim( $post->post_title )
)
{
return; // Nothing to do.
}
$this->post = $post;
foreach ( $this->searches as $search => $properties )
{
if ( $this->has_match( $search, $properties[\'search_type\'] ) )
{
$this->add_term( $properties[\'term\'], $properties[\'taxonomy\'] );
}
}
$this->write_terms();
}
/**
* Does the title contain our search phrase?
*
* @param string $search
* @param array $type
* @return bool
*/
protected function has_match( $search, $type )
{
switch ( $type )
{
case \'case-insensitive\':
// It may return 0, but that\'s not the same as FALSE.
return FALSE !== stripos( $this->post->post_title, $search );
case \'regex\':
return preg_match( $search, $this->post->post_title, $m );
default:
return FALSE !== strpos( $this->post->post_title, $search );
}
}
/**
* Adds the term to the post.
*
* @param string $term
* @param string $taxonomy
* @return void
*/
protected function add_term( $term, $taxonomy )
{
if ( empty ( $this->update[ $taxonomy ] ) )
{
$this->update[ $taxonomy ] = wp_get_object_terms(
$this->post->ID,
$taxonomy,
array ( \'fields\' => \'name\' )
);
}
if ( get_term_by( \'name\', $term, $taxonomy ) )
{
$this->update[ $taxonomy ][] = $term;
$this->update[ $taxonomy ] = array_unique( $this->update[ $taxonomy ] );
}
}
/**
* Takes all terms from $update and relates them to the current post.
*
* @return void
*/
protected function write_terms()
{
if ( ! empty ( $this->update ) )
{
foreach ( $this->update as $taxonomy => $terms )
{
wp_set_object_terms( $this->post->ID, $terms, $taxonomy );
}
}
}
}
在这里,我写了一篇标题为
a test with wORdpReSs and 22kg Dog Training 然后我按了一次“保存草稿”:
到
update existing posts 运行以下操作一次:
$posts = get_posts( array ( \'numberposts\' => - 1 ) );
foreach ( $posts as $post )
{
new WPSE_39700_Term_By_Title( $post->ID, $post );
}