Asked 30+ days ago
27 Jun 2017
Views 257
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 Jan 21 '18 20:17

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..
Edit Answer
Share
Post Answer