如何根据标题为帖子添加术语?

时间:2012-01-21 作者:mashup

我想添加一个特定类别的所有帖子标题中带有关键字“狗训练”。

原因:如果你在WordPress搜索中输入了一些内容,那么它就不完全匹配。以前的WordPress搜索要准确得多,但现在他们放弃了。注意单词之间的空格。

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

您必须首先检查现有条款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 然后我按了一次“保存草稿”:

enter image description here

update existing posts 运行以下操作一次:

$posts = get_posts( array ( \'numberposts\' => - 1 ) );
foreach ( $posts as $post )
{
    new WPSE_39700_Term_By_Title( $post->ID, $post );
}

结束

相关推荐

Pagination on custom taxonomy

我很难做到这一点。这不是我第一次遇到分页问题,或者说是WPs URL系统的问题。基本上我有这个URL:http://example.com/location/dc 并加载taxonomy-location.php 样板现在,我将为主题添加分页功能。所以我有一个URL:http://example.com/location/dc/page/2 它不会加载taxonomy-location.php 模板,它实际上加载404 样板这是调试栏显示的内容:看起来它得到了正确的值,WP只是没有加