Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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 to query this post by slug?

1 Answer

0 votes
by (106k points)

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

<?php

$the_slug = 'my_slug';

$args = array(

'name' => $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;?>   

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer
+1 vote
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...