WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to List Private Posts as Well as Published Ones (6 posts)

  1. cmarshall
    Member
    Posted 6 years ago #

    Okay, I've written a plugin that lists new posts as an addition to a page. It creates a "mini-loop" and lists the latest posts. This allows me to have a page (static home page) with a bunch of blather, and then a list of the latest posts under it.

    What I want to be able to do is list published and private posts (of course, the private ones only show up if current_user_can('read_private_posts')).

    Here's the basic code:

    $posts = get_posts('');
    ?><div class="listposts_new_posts">
     <div class="listposts_header">Latest Information</div><?php
     while ( $mypost ) {

    You get the idea. Very simple. The problem is that get_posts() only returns published posts.

    I have spent all day searching the codex to figure out how I can add private posts to the returned posts, and I am now even more mixed up than I was when I started.

    Any ideas? You can just tell me what M in RTFM I should read.

    Thanks!

  2. cmarshall
    Member
    Posted 6 years ago #

    Well, I solved it, but it would have been nice to do it in a less "hacky" fashion:

    function lp_get_posts()
    {
    	global $wpdb;
    	$today = current_time('mysql', 1);
    
    	return $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql('post') . " AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 5");
    }
    
    function listposts ( $in_content )
    {
    	if ( preg_match ( "/<!-- LIST_POSTS -->/", $in_content ) )
    		{
    		$posts = lp_get_posts();
  3. gspark
    Member
    Posted 6 years ago #

    so how did you use this code in your template ?

  4. cmarshall
    Member
    Posted 6 years ago #

    Here's the whole kit n' kaboodle. Open wide...

    <?php
    /*
    Plugin Name: ListPosts
    Version: 1.0
    Plugin URI: http://www.example.com
    Description: Lists new posts like the front page.. Add <!- - LIST_POSTS - -> to a page or a post to generate the form.
    Author: Example
    Author URI: http://www.exa,ple.com
    */
    function lp_get_the_password_form($id) {
    	$id = "password_$id";
    	$output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
    	<p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
    	<p><label for="'.__($id).'">' . __("Password:") . '</label> <input name="post_password" id="'.__($id).'" type="password" size="20" /> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
    	</form>
    	';
    	return $output;
    }
    function lp_get_posts()
    {
    	global $wpdb;
    	$today = current_time('mysql', 1);
    
    	return $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql('post') . " AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 5");
    }
    
    function listposts ( $in_content )
    {
    	if ( preg_match ( "/<!-- LIST_POSTS -->/", $in_content ) )
    		{
    		$posts = lp_get_posts();
    		ob_start();
    			?><div class="listposts_new_posts">
    			<div class="listposts_header">Latest Information</div><?php
    				ec3_filter_the_posts ( $posts );
    				$mypost = current ( $posts );
    				while ( $mypost )
    					{
    					if ( !$mypost->ec3_schedule )
    						{
    						$url = get_permalink( $mypost->ID );
    						$title = htmlspecialchars( $mypost->post_title );
    						$meta = '';
    						$cats = wp_get_post_categories ( $mypost->ID );
    						foreach ( $cats as $cat )
    							{
    							if ( $meta )
    								{
    								$meta .= ", ";
    								}
    							$meta .= '<a href="' . get_category_link($cat) . '" title="' . sprintf(__("View all posts in %s"), get_the_category_by_ID($cat)) . '">'.get_the_category_by_ID($cat).'</a>';
    							}
    						ec3_filter_the_content ( $mypost->post_content );
    						$content = $mypost->post_content;
    						if ( !empty($mypost->post_password) )
    							{ // if there's a password
    							if ( stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $mypost->post_password )
    								{	// and it doesn't match the cookie
    								$content = lp_get_the_password_form($mypost->ID);
    								}
    							}
    						$content = apply_filters ( 'the_content', $content );
    						$content = str_replace(']]>', ']]>', $content);
    						$content = get_extended ( $content );
    						echo '<div class="listposts_post">';
    							echo '<div class="listposts_post_title"><a href="'.$url.'">';
    							echo $title;
    							echo '</a></div>';
    							if ( $meta )
    								{
    								echo '<div class="listposts_category_meta">Posted in '.$meta.'</div>';
    								}
    							echo '<div class="listposts_post_teaser">'.$content['main'];
    							if ( $content['extended'] )
    								{
    								echo '<div class="listposts_post_more"><a href="'.$url.'">Read More...</a></div>';
    								}
    
    							$mypost = next ( $posts );
    
    							if ( $mypost )
    								{
    								echo '<div class="post_separator"></div>';
    								}
    							echo '</div>';
    						}
    					else
    						{
    						$mypost = next ( $posts );
    						}
    					echo '</div>';
    					}
    			$display = ob_get_contents();
    		ob_end_clean();
    		$in_content = preg_replace ( "/(<p[^>]*>)*?<!-- LIST_POSTS -->(<\/p[^>]*>)/", $display, $in_content );
    		}
    
    	return $in_content;
    }
    add_filter ( 'the_content', 'listposts' );
    ?>
  5. cmarshall
    Member
    Posted 6 years ago #

    Just FYI. I just fixed an error:

    else
    						{
    						$mypost = next ( $posts );
    						}
    					echo '</div>';
    					}
    /* THIS IS THE FIX */		echo '</div> <!-- listposts_new_posts -->';
    			$display = ob_get_contents();

    You can delete the calendar stuff. I added a filter to remove eventcalendar3 entries.

  6. Greg Farries
    Member
    Posted 6 years ago #

    I've tried to install this plugin/code, but it didn't work as intended.

    Has anyone figured out a better way to display private postings?

Topic Closed

This topic has been closed to new replies.

About this Topic