Forums

List of selected posts... (11 posts)

  1. manojit
    Member
    Posted 4 years ago #

    Is there a simple php command to show a list of selected posts by their IDs? For example, here I hand-coded the list, but modifying it is more work than just changing IDs.
    I did give a forum search, all I came up with is this unresolved thread from 4 months ago.
    Thanks.

  2. Austin Matzko
    Member
    Posted 4 years ago #

    Is there a simple php command to show a list of selected posts by their IDs?

    Yes. The following does that, where the numbers are post IDs:

    $posts = get_posts(array('include' => '345,23,678,2'));

  3. manojit
    Member
    Posted 4 years ago #

    This looks simple. Where do I put this code to make a separate page of my list of posts? Thanks!

  4. moshu
    Member
    Posted 4 years ago #

    On that Page's own custom Page template file.

  5. manojit
    Member
    Posted 4 years ago #

    Thanks, moshu.

  6. manojit
    Member
    Posted 4 years ago #

    Okay, following moshu's suggestion, I created a custom page template 'mostviewed.php' by copying page.php, and here is the relevant piece that uses the 'get_posts()' command suggested above:

    <h2><?php the_title(); ?></h2>
    
    			<div class="entry">
    Here are some of my posts that are visited more often than others.
    
    <?php get_posts(array('include' => '70,44,48,21,24,22,11,65,51,96,97,40,91')); ?>
    
    				<?php the_content('<p class="serif">Read the rest of this page &raquo;
    '); ?>
    		<?php link_pages('<strong>Pages:</strong> ', '
    ', 'number'); ?>
    			</div>

    Then I created an empty page with this page template. But I still get this empty page, and not this that I wanted (post IDs in get_posts() correspond exactly to this list). What am I doing wrong? Thanks.

  7. Austin Matzko
    Member
    Posted 4 years ago #

    I think what you really want is for the posts to show in what WordPress calls the "Loop". get_posts has no effect on that, but query_posts does. So you could put the following code in place of the get_posts call above:

    <?php add_filter('posts_where', create_function('$where','global $wpdb; $where .= " AND $wpdb->posts.ID IN (70,44,48,21,24,22,11,65,51,96,97,40,91) "; return $where;'));
            query_posts('orderby=date'); ?>
  8. manojit
    Member
    Posted 4 years ago #

    filosofo, thanks for your time and effort. After doing as you suggested, the page seems to take forever to load. Any more thoughts?
    Thanks again for your help.

  9. manojit
    Member
    Posted 4 years ago #

    After waiting it out, finally my firefox flashed an Internal Server Error ("The server encountered an internal error or misconfiguration and was unable to complete your request.")

  10. Austin Matzko
    Member
    Posted 4 years ago #

    I should have said that the code I posted in my last comment needs to go just before the "Loop," in other words before have_posts(), not where the get_posts call is in your example. Sorry about that.

  11. manojit
    Member
    Posted 4 years ago #

    filosofo, thanks again. The page is better now, but, 1) it shows the entire post rather than just the title (with link) that I wanted, and, 2) the list is a small subset of the post IDs that I supplied. Anything more?

Topic Closed

This topic has been closed to new replies.

About this Topic