WP_Query Clarification please
-
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 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘WP_Query Clarification please’ is closed to new replies.