Asked 7 years ago
27 Jun 2017
Views 4604
Cimb

Cimb posted

Difference between get_post() and the_post() in wordpress ?

what is Difference between get_post() and the_post() ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

in wordpress , get_post() is used to get the post data for given post id or post object

$post = get_post( 10 ); 



in wordpress , the_post() is used to iterate the index in loop . it means it used like increment index by each iterated

 while ( have_posts() ) : the_post();
        if (
            ( $this->is_trash && $post->post_status != 'trash' )
            || ( ! $this->is_trash && $post->post_status === 'trash' )
        ) {
            continue;
        }
        $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
    ?>
        <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
            <?php $this->single_row_columns( $post ); ?>
        </tr>
    <?php
    endwhile;


in above code have_posts() is used to check is there iteration at end or not . if not than the_post() is used to iterated index to next post so we can access the property of post object by function like

 while ( have_posts() ) : the_post();
 echo the_title();// print title 
 echo the_content(); // print content
 endwhile;

or by $post object property directory like this

 while ( have_posts() ) : the_post();
 echo  $post->ID;// print ID
 echo $post->post_status; // print post status
 endwhile;


etc..
Post Answer