WordPress.org

Ready to get started?Download WordPress

Forums

Custom SELECT query to pull recent comments AND recent posts (1 post)

  1. Matthew Gerring
    Member
    Posted 5 years ago #

    Hello all- I'm looking for a way to have an "activity feed" in the sidebar of my blog that combines recent posts as well as recent comments.

    I have the recent comments plugin and the recent posts plugin, and both of them work via a custom SELECT query that looks like this-

    recent_comments.php:

    global $wpdb;
        $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') ";
    	if(!$show_pass_post) $request .= "AND post_password ='' ";
    	$request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $no_comments";
    	$comments = $wpdb->get_results($request);

    recent_posts.php:

    global $wpdb;
    	$time_difference = get_settings('gmt_offset');
    	$now = gmdate("Y-m-d H:i:s",time());
        $request = "SELECT ID, post_title, post_excerpt 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 = '';

    What I want to know is, what's the proper syntax for combining the first and second requests so that I'm pulling recent comments and recent posts, and displaying them in chronological order in the sidebar? And is there a way to identify posts vs. comments if I do this?

Topic Closed

This topic has been closed to new replies.

About this Topic