WordPress query single post by slug

903    Asked by EdythFerrill in Python , Asked on Jul 5, 2021

For the moment when I want to show a single post without using a loop I use this:

<?php
$post_id = 54;
$queried_post = get_post($post_id);
echo $queried_post->post_title; ?>

The problem is that when I move the site, the id's usually changing. Is there a way in WordPress get post by slug?

Answered by Danna sahi

If you want to show the WordPress query single post by slug you can use the below-mentioned code: $the_slug,

'post_type' =>'Post',
'post_status' =>'Publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
if( $my_posts ) :
echo 'ID on the first post found '. $my_posts[0]->ID; endif;?>


Your Answer

Interviews

Parent Categories