Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I found the following codes to get post details. However, it is not possible to get the posts by content. Does any one has any ideas?

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;
share|improve this question
    
Thanks for all of your help. They have been useful. However considering the performce, the answer of m0r7if3r + janw is better. –  goodseller Apr 19 '12 at 12:21
    
Just a hint for your next question: If you really just want the ID and no other fields then don't ask for other fields. –  toscho Apr 19 '12 at 13:40
    
@goodseller As you wrote »considering the performance«: How exactly did you measure the it? –  kaiser Apr 20 '12 at 4:36
    
Sorry that I am new newbie in wp. toscho's should be better. –  goodseller Apr 26 '12 at 5:54

2 Answers 2

up vote 4 down vote accepted

You have to extend the db query to search in the post_content column. There is a filter: 'posts_where' you can use.

I would write a simple wrapper for get_posts() to extends its arguments an run the filter once. Example:

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";
    }
}

To find the last five posts containing the string resized write:

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

To get the last five posts not containing resized:

$args = array(
    'content' => '%resized%',
    'like'    => FALSE
);
$posts = T5_Posts_By_Content::get( $args );
share|improve this answer

I can't think of an elegant way off the top of my head, I'm not sure if it's even possible using core functions, but the SQL you would want to use would be something like this (note that this is pseudocode):

SELECT  `ID` 
FROM  `$wpdb->posts` 
WHERE  `post_content` LIKE  '%search term%'

This will return all the IDs of posts that SQL thinks relates to your search term.

share|improve this answer
2  
remember to but it in $wpdb codex.wordpress.org/Class_Reference/wpdb –  janw Apr 19 '12 at 11:00
    
And also don't forget to do a) $wpdb->prepare() and like_escape(). –  kaiser Apr 20 '12 at 4:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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