How to get posts by content?

时间:2012-04-19 作者:goodseller

我找到了以下代码来获取帖子详细信息。但是,不可能get the posts by content.有人有什么想法吗?

get_posts()

//// get post ////
$args = array(
    \'numberposts\'     => -1, // number of posts to display; display all: -1.
    \'offset\'          => 0,
    // \'category\'        => , // post category ID
    \'orderby\'         => \'post_date\',
    \'order\'           => \'DESC\', // Latest post first: \'ASC\'; Olderest post first: \'DESC\'
    // \'include\'         => ,
    // \'exclude\'         => ,
    // \'meta_key\'        => ,
    // \'meta_value\'      => ,
    \'post_type\'       => \'post\', // get post type
    // \'post_mime_type\'  => ,
    // \'post_parent\'     => ,
    // \'post_status\'     => \'publish\'
);

// http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
$posts_array = get_posts( $args );
foreach( $posts_array as $post ) : setup_postdata($post);
    echo "<li><a href=\'" . the_permalink() . "\'>" . the_title() .
         "</a>" .
         $post->blog_ID . 
         $post->post_date . 
         $post->post_title . 
         $post->pubtimes . 
         $post->post_author . 
         $post->post_content . 
         $post->post_excerpt . 
         $post->post_status;
        "</li>";
endforeach;

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

您必须扩展db查询才能在post_content 柱有一个过滤器:\'posts_where\' 您可以使用。

我会为get_posts() 要扩展其参数,请运行一次筛选器。示例:

class T5_Posts_By_Content
{
    protected static $content = \'\';

    protected static $like    = TRUE;

    /**
     * Mapper for get_posts() with extra arguments \'content\' and \'like\'
     *
     * \'content\' must be a string with optional \'%\' for free values.
     * \'like\' must be TRUE or FALSE.
     *
     * @param array $args See get_posts.
     * @return array
     */
    public static function get( $args )
    {
        if ( isset ( $args[\'content\'] ) )
        {
            // This is TRUE by default for get_posts().
            // We need FALSE to let the WHERE filter do its work.
            $args[\'suppress_filters\'] = FALSE;
            self::$content            = $args[\'content\'];
            add_filter( \'posts_where\', array ( __CLASS__, \'where_filter\' ) );
        }

        isset ( $args[\'like\'] ) and self::$like = (bool) $like;

        return get_posts( $args );
    }

    /**
     * Changes the WHERE clause.
     *
     * @param string $where
     * @return string
     */
    public static function where_filter( $where )
    {
        // Make sure we run this just once.
        remove_filter( \'posts_where\', array ( __CLASS__, \'where_filter\' ) );

        global $wpdb;
        $like  = self::$like ? \'LIKE\' : \'NOT LIKE\';
        // Escape the searched text.
        $extra = $wpdb->prepare( \'%s\', self::$content );

        // Reset vars for the next use.
        self::$content = \'\';
        self::$like    = TRUE;

        return "$where AND post_content $like $extra";
    }
}
查找包含字符串的最后五篇帖子resized 写入:

$args = array(
    \'content\' => \'%resized%\'
);
$posts = T5_Posts_By_Content::get( $args );
获取最后五篇帖子not 包含resized:

$args = array(
    \'content\' => \'%resized%\',
    \'like\'    => FALSE
);
$posts = T5_Posts_By_Content::get( $args );

SO网友:mor7ifer

我想不出一个优雅的方法,我甚至不确定是否可以使用核心函数,但您想要使用的SQL应该是这样的(请注意,这是伪代码):

SELECT  `ID` 
FROM  `$wpdb->posts` 
WHERE  `post_content` LIKE  \'%search term%\'
这将返回SQL认为与您的搜索词相关的帖子的所有ID。

结束