Fastest way to get 1 post->ID in WordPress

So all you need is a post->ID whats the fastest way to get it ?

Jump to conclusion or lets evaluate our options here:

query_posts(), get_posts(),wp_query(), $wpdb

As a rule of thumb, usually we can say $wpdb > wp_query > get_posts > query_posts, meaning; you should almost never ever use query_posts. And usually between the two, wp_query. performs better over get_posts in most situations, and querying the db directly via $wpdb is often the most efficient way eventhough wp_query is safer.

For results there is no difference, you will end up with 1 ID.

gp1idsspUntitled-1

Like I said above query_posts is almost never needed, lets get that out of the way with a simple query_posts vs get_posts test:

gpvssgpp

That leaves us with wp_query, get_posts and $wpdb. Lets take a look at get_posts vs wp_query. (Please note that I’ve used 3 different WP_query args sets, you can see all the functions below)

gg2Clipboard01

The difference is not all that big, but get_posts is the winner compared to __wp_query_add_args_no_cache .( wpquery with additional args for optimal performance )

So lets pick a winner is it gonna be get_posts or $wpdb ? As expected $wpdb is the clear winner here:

wpdbwwUntitled-1

Concluison: If all you need is 1 random post->ID, simply use $wpdb

Here are all the functions used:

	function eo_get_an_id_queryposts() {
		$get_one_id_args = array(
			'orderby' => 'rand',
			'posts_per_page' => '1',
			'fields' => 'ids'
		);
		$rand_post_id_arr = query_posts( $get_one_id_args );
		$rand_post_id = $rand_post_id_arr[0];
		// intval it because id returns as string
		return intval($rand_post_id);
	}
	function eo_get_an_id_getposts() {
		$get_one_id_args = array(
			'orderby' => 'rand',
			'posts_per_page' => '1',
			'fields' => 'ids'
		);
		$rand_post_id_arr = get_posts( $get_one_id_args );
		$rand_post_id = $rand_post_id_arr[0];
		// intval it because id returns as string
		return intval($rand_post_id);
	}
	
	function eo_get_an_id_wpquery_min_args() {
		$get_one_id_args = array(
			'orderby' => 'rand',
			'posts_per_page' => '1',
		);
		$get_an_id_qy = new WP_Query( $get_one_id_args );
		$get_an_id_qy_result_arr = $get_an_id_qy->posts;
		// no point on going forward if no posts found.
		if( empty($get_an_id_qy_result_arr) ) return false;
		// Get the->ID from the post object.
		$rand_post_obj = $get_an_id_qy_result_arr[0];
		$rand_post_id = $rand_post_obj->ID ;
		return $rand_post_id;
	}
	function eo_get_an_id_wpquery_add_args() {
		$get_one_id_args = array(
			'orderby' => 'rand',
			'posts_per_page' => '1',
			'fields' => 'ids'
		);
		$get_an_id_qy = new WP_Query( $get_one_id_args );
		$get_an_id_qy_result_arr = $get_an_id_qy->posts;
		// no point on going forward if no posts found.
		if( empty($get_an_id_qy_result_arr) ) return false;
		$rand_post_id = $get_an_id_qy_result_arr[0];
		// intval it because id returns as string
		return intval($rand_post_id);

	}
	
	function eo_get_an_id_wpquery_add_args_nocache() {
		$get_one_id_args = array(
			'orderby' => 'rand',
			'posts_per_page' => '1',
			'fields' => 'ids',
			'no_found_rows' => true,
			'cache_results'=>false,
			'update_post_term_cache' => false,
			'update_post_meta_cache' => false,
		);
		$get_an_id_qy = new WP_Query( $get_one_id_args );
		$get_an_id_qy_result_arr = $get_an_id_qy->posts;
		// no point on going forward if no posts found.
		if( empty($get_an_id_qy_result_arr) ) return false;
		$rand_post_id = $get_an_id_qy_result_arr[0];
		// intval it because id returns as string
		return intval($rand_post_id);

	}
	function eo_get_an_id_add_wpdb() {
		global $wpdb;
		$rand_post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY RAND() LIMIT 1");
		// intval it because id returns as string
		return intval($rand_post_id);
	}

Leave a Reply

Your email address will not be published.