0 votes
1 view
in Web Technology by (47.7k 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 (107k 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
+1 vote
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...