How to get post by slug in Wordpress?

0.00
22:19
2346
Hello guys! I need to get WP post ID by its slug. Is it possible? If so, can anybody help me with function?
Related CMS:
22:21
Hi Tony. The easiest way to get post ID by slug:
 
<?php
   $queried_post = get_page_by_path('my_slug',OBJECT,'post');
?>
 

Also, maybe recommendation from Codex will be helpful for you:
 
<?php
$the_slug = 'my-slug';
$args = array(
	'name'           => $the_slug,
	'post_type'      => 'post',
	'post_status'    => 'publish',
	'posts_per_page' => 1
);
$my_posts = get_posts( $args );
if( $my_posts ) {
	echo 'ID on the first post found ' . $my_posts[0]->ID;
}
?>
 

BTW, why you need to get post ID by slug?

Write a comment

Loading...