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.

Hi this is the code which displays recent post from all category. but I want to dispaly post from any single category. I dont want to change the code please help.

function mdv_recent_posts( $no_posts = 5, $before = '<li>', $after = '</li>', $hide_pass_post = true, $skip_posts = 0, $show_excerpts = false, $include_pages = false ) {
global $wpdb;


$time_difference = get_settings( 'gmt_offset' );
$now = gmdate( "Y-m-d H:i:s", time() );
$request = "SELECT ID, post_title, post_excerpt, post_date, comment_count FROM $wpdb->posts WHERE post_status = 'publish' ";


if ( $hide_pass_post )
    $request .= "AND post_password ='' ";
if ( $include_pages )
    $request .= "AND (post_type='post' OR post_type='page') ";
else
    $request .= "AND post_type='post' ";
$request .= "AND post_date_gmt < '$now' ORDER BY post_date DESC LIMIT $skip_posts, $no_posts";
$posts = $wpdb->get_results( $request );
$output = '';

if ( $posts ) {
    foreach ( $posts as $post ) {
        $post_title = $post->post_title;
        $permalink = get_permalink( $post->ID );
        $date=date('F j, Y', strtotime($post->post_date));
        $output .= $before . '<span class="date">'.$date.'</span><a href="' . esc_url( $permalink ) . '" rel="bookmark" title="Permanent Link: ' . esc_attr( $post_title ) . '">' . esc_html( $post_title ) ;
        if ( $show_excerpts ) {
            $post_excerpt = esc_html( $post->post_excerpt );
            $output.= '<br />' . $post_excerpt;
        }
        $output .= $after;
    }
} else {
    $output .= $before . "None found" . $after;
}
echo $output;
}
share|improve this question
add comment

1 Answer

The function you have posted uses a nasty custom SQL query which is really not necessary and adding a category filter to it would make it even worse, you can do all of the filters using WP_Query object or get_posts except the exclusion of password protected posts which can be done with a simple function hooked to posts_where filter so here you go:

//function to exclude password protected posts
function exclude_pw_where($where) {
    $where .= " AND post_password = '' ";
    return $where;
}

// function to get latest posts of category
function latest_posts_of_category($no_posts = 5, $before = '<li>', $after = '</li>', $hide_pass_post = true, $skip_posts = 0, $show_excerpts = false, $include_pages = false, $cat_id = null){
    global $post;
    $temp = $post;
    $args = array(
        'posts_per_page' => $no_posts,
        'post_type' => 'post',
        'post_status' => 'published',
        'order' => 'DESC',
        'orderby' => 'date',
        'offset' => $skip_posts,
    );

    if ( $cat_id != null )
        $args['cat'] = $cat_id;     

    if ( $include_pages )
        $args['post_type'] = array('post','page');

    //add filter to exclude password protected posts
    if ( $hide_pass_post )
        add_filter('posts_where', 'exclude_pw_where');

    $posts = get_posts($args);
    if ( $posts ) {
        foreach ( $posts as $post ) {
            $post_title = $post->post_title;
            $permalink = get_permalink( $post->ID );
            $date=date('F j, Y', strtotime($post->post_date));
            $output .= $before . '<span class="date">'.$date.'</span><a href="' . esc_url( $permalink ) . '" rel="bookmark" title="Permanent Link: ' . esc_attr( $post_title ) . '">' . esc_html( $post_title ) ;
            if ( $show_excerpts ) {
                $post_excerpt = esc_html( $post->post_excerpt );
                $output.= '<br />' . $post_excerpt;
            }
            $output .= $after;
        }
    } else {
        $output .= $before . "None found" . $after;
    }
    echo $output;

    //remove the filter if was called
    if ( $hide_pass_post )
        remove_filter('posts_where', 'exclude_pw_where');

}

it does the same thing but in a much safer way and you can set the last parameter $cat_id to specify a category (or a list of categories using an array) by the category id.

share|improve this answer
    
It shows the post in the list which has been removed to trash as well how to correct it. –  Suman Gaudel Sep 21 '11 at 15:09
    
change 'post_status' => 'published', to 'post_status' => 'publish', –  Bainternet Sep 21 '11 at 15:23
add comment

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.