How to Get Post ID in WordPress

In this blog post I will share different methods from which you can get the post_id, whether for pages, custom post types or even WooCommerce products and orders.

1. In the URL When Editing

while editing a post/page you can simply check the URL and get the post ID from there.

It is not even necessary to open a post-edit page. Just move the mouse over the «Edit» link or the post title in the admin area and then look at the browser status bar (bottom left part of the screen).

2. Add a Custom Column of post-id

If you need post IDs a lot then by using a code snippet you can easily add the post ID column in all posts table.

add_filter( 'manage_posts_columns', 'wpexpertguide_add_post_id_column' );
add_action( 'manage_posts_custom_column', 'wpexpertguide_display_post_id', 10, 2 );

function wpexpertguide_add_post_id_column( $columns ) {
    $columns['post_id'] = 'Post ID';
    return $columns;
}

function wpexpertguide_display_post_id( $column, $post_id ) {
    if ( 'post_id' == $column ) {
        echo $post_id;
    }
}

you have to add the above code in the child-theme functions.php file or you can use the code snippets plugin and add that in a PHP snippet file.

3. Get post ID in a loop

Sometimes you will need to get the post ID in a posts loop. you were either using get_posts()WP_Query or query_posts(). Let’s see how we can get the post-ids.

3.1 get_posts() 

$blog_posts = get_posts( array( 'posts_per_page' => 50 ) );
foreach( $blog_posts as $post ) {
	echo $post->ID;
}

3.2 WP_Query

$query = new WP_Query( array( 'posts_per_page' => -1 ) );
while( $query->have_posts() ) : $query->the_post();
	the_ID();
	// or: echo get_the_ID();
	// or: echo $query->post->ID;
endwhile;

So, you see that there are 3 ways how you can get a post ID in a loop created with WP_Query!

  1. the_ID() function uses the global $post variable which is installed from an object available in $query->post property.
  2. get_the_ID() is similar but it returns the result not prints it.
  3. $q->post->ID is just a post ID obtained directly from an object.

4. Get Post ID of a current post

Here are some other ways you can get the post IDs e-g while working on single.php or page.php file you can get the post ID with global post variable

global $post;
echo $post->ID;

Here is another way of doing it that returns the ID of any kind of object which page is displaying right now. So it can be used to get a current category or tag ID as well.

echo get_queried_object_id();

5. Get post ID from permalink (pretty URL) 

You can use the function url_to_postid to get the ID from URL. It works great especially when pretty permalinks are turned on.

$post_id = url_to_postid( 'https://wpexpertguide.com/wordpress/get-post-id.html' );

6. Get post ID by title 

There is a built-in WordPress function and since 3.0 version it works not only for pages but for any custom post type. The Third function parameter is name of a post type (page by default).

$my_post = get_page_by_title( 'Hello World', '', 'post' );
echo $my_post->ID;

7. Get post ID by slug

The function is similar to get_page_by_title(). But if your post has parent (for hierarchical post types only) you have to specify parent slug as well, for example parent-post/hello-world.

$my_post = get_page_by_path( 'hello-world', '', 'post' );
echo $my_post->ID;

8. By a specific pair of meta key and value 

Retrieve post IDs based on specific meta key-value pairs with SQL queries.

global $wpdb;
 
$ids = $wpdb->get_col( 
	$wpdb->prepare( 
		"
		SELECT post_id 
		FROM $wpdb->postmeta 
		WHERE meta_key = %s AND meta_value = %s
		", 
		$meta_key, 
		$meta_value 
	)
);

// consider that there could be multiple posts with the same meta key and value
// that's why we didn't use $wpdb->get_var();
if( count( $ids ) > 1 ) {
	print_r( $ids );
} else {
	echo reset( $ids ); // first element of an array
}

By understanding these above methods you’ll never struggle to find post IDs in WordPress again. Whether you’re a seasoned developer or a WordPress newbie these techniques provide a range of options to suit your needs. Say goodbye to frustration and hello to efficiency with these tried-and-tested approaches.

Leave a Reply

Your email address will not be published. Required fields are marked *