How to not repeat wordpress loop/query?

This is a bad habit i’ve been using in wordpress and i want to stop. I repeat the loop/query code when i should fetch all at once. Here is an example…

 <?php
                $args = array(
                'p' => 9345, // id of a page, post, or custom type
                'post_type' => 'front page content');

                $loop = new WP_Query( $args );

                //Display the contents

                while ( $loop->have_posts() ) : $loop->the_post();
                the_content(); 
     endwhile; 

                $args = array(
                'p' => 9341, // id of a page, post, or custom type
                'post_type' => 'front page content');

                $loop = new WP_Query( $args );

                //Display the contents

                while ( $loop->have_posts() ) : $loop->the_post(); 

     the_content();  endwhile; 
?>

If i want to display the content of the id 9345 first and the content of the id 9341 second, how do i do this all without duplicating WP_Query? I know about order_by but what if i want to specifically order results from the query?

Related posts

Leave a Reply

1 comment

  1. You can use post__in, Read more

    <?php
        $args = array(
            'post__in' => array(9345, 9341),
            'post_type' => 'page'
        )
    
        $loop = new WP_Query( $args );
    
        //Display the contents
    
        while ( $loop->have_posts() ) : $loop->the_post();
            the_content(); 
        endwhile; 
    
        //DONT FORGET THIS
        wp_reset_postdata();
    ?>