Md Mehedi Hasan

get_posts | custom query

post__in

<?php
/*
* Template Name: Custom Query
*/

?>

<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">

    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $post_ids =array(50, 22, 31, 37, 62, 57, 67);
    $_P = get_posts(array(
        'post__in' => $post_ids,
        'order' => 'post__in',
        'posts_per_page' => $post_per_page,
        'paged' => $paged

    ));

    foreach ($_P as $post) {
        setup_postdata($post);
    ?>
        <h2> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php
    }
    wp_reset_postdata();
    ?>


    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    "total"=> ceil( count( $post_ids ) / $post_per_page),
                    "screen_reader_text" => ' ',
                    "prev_text" => "New Posts",
                    "next_text" => "Old Posts"
                ));
                ?>
            </div>
        </div>
    </div>


</div>


<?php get_footer(); ?>

author__in

<?php
/*
* Template Name: Custom Query
*/

?>

<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">

    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $total= 9;
    $_P = get_posts(array(
        'autho__in' => array(1),
        'order' => 'post__in',
        'total' => 9,
        'posts_per_page' => $post_per_page,
        'paged' => $paged

    ));

    foreach ($_P as $post) {
        setup_postdata($post);
    ?>
        <h2> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php
    }
    wp_reset_postdata();
    ?>


    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    "total"=> ceil( $total / $post_per_page),
                    "screen_reader_text" => ' ',
                    "prev_text" => "New Posts",
                    "next_text" => "Old Posts"
                ));
                ?>
            </div>
        </div>
    </div>


</div>


<?php get_footer(); ?>

without setup_postdata (not recommend)

<?php
/*
* Template Name: Custom Query
*/

?>

<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">

    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $total= 9;
    $_P = get_posts(array(
        'autho__in' => array(1),
        'order' => 'post__in',
        'total' => 9,
        'posts_per_page' => $post_per_page,
        'paged' => $paged

    ));

    foreach ($_P as $p) {
    ?>
    <h2> <a href="<?php echo esc_url($p->guid) ?>">
    <?php
    echo apply_filters("the_title", $p->post_title);
    ?></a>
    </h2>
    <?php
    
    } 
    ?>


    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    "total"=> ceil( $total / $post_per_page),
                    "screen_reader_text" => ' ',
                    "prev_text" => "New Posts",
                    "next_text" => "Old Posts"
                ));
                ?>
            </div>
        </div>
    </div>


</div>


<?php get_footer(); ?>