To get the post ID by the slug in WordPress, you can use the get_page_by_path function. This function returns an object with the post data when given a slug, and you can then access the ID of the post using the ID property of the object.
Here’s an example of how you can use this function:
$slug = 'my-post-slug'; $post = get_page_by_path( $slug, OBJECT, 'post' ); $post_id = $post->ID;
This code will get the post object for the post with the slug my-post-slug, and then it will retrieve the ID of the post and store it in the $post_id variable.
You can also use the get_page_by_path function to get a page by its slug by passing 'page' as the second argument instead of 'post'.
I hope this helps!



