Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

WP_Query Clarification please

$
0
0

Replies: 0

I’m confused about the bext way to get the post ID inside a WP_Query loop. Of the two methods below, I’ve read that the first is less prone to error than the second but also less efficient. Is this correct? Which is the best one to use please?

Method One

$query = array( 
    'post_type' => 'my-custom-post-type',
    'post_status' => 'publish'
);
    
$result = new WP_Query( $query );
if ( $result->have_posts() ) {
    while ( $result->have_posts() ) {        
        $result->the_post();
        $result_id = (int) get_the_ID();		
    }
}
wp_reset_postdata();

Method Two

$query = array( 
    'post_type' => 'my-custom-post-type',
    'post_status' => 'publish'
);
    
$result = new WP_Query( $query );
if ( $result->have_posts() ) {
    while ( $result->have_posts() ) {        
        $result->the_post();
        $result_id = (int) $result->post->ID;	
    }
}
wp_reset_postdata();

Many thanks


Viewing all articles
Browse latest Browse all 59525

Trending Articles