Forums

[resolved] How to show certain posts on a page? (4 posts)

  1. seanjacob
    Member
    Posted 1 year ago #

    I know how to show the latest posts, but I want to get 3 posts by calling their IDs.

    <?php
    $lastposts = get_posts('numberposts=12');
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>

    Thank you.

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    try the 'post__in' parameter:

    http://codex.wordpress.org/Function_Reference/WP_Query#Post_.26_Page_Parameters

    the query parameters are supposed to work with 'get_posts()' as well.

    for example:

    <?php
    $args = array(
    'numberposts' => 12,
    'post__in' => array( 3, 17, 234)
    );
    $lastposts = get_posts( $args );
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>
  3. seanjacob
    Member
    Posted 1 year ago #

    Thanks alchymyth, I manage to get the posts I want but I am having trouble displaying them in the order I input them in.

    <?php
    $the_query = new WP_Query( array( 'post__in' => array( 44, 18, 36 ), 'orderby' => 'none' ) );
    if( have_posts() ) :
    while ($the_query->have_posts()) : $the_query->the_post();
    ?>

    Any ideas?

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    you may need to use three 'get_post()' functions -
    afaik, there is no way to have the posts show in any particular order, other than the ones provided by the query (date, title, ..)

Topic Closed

This topic has been closed to new replies.

About this Topic