i want to send post slug through URL and get on other page. e.g, www.example.com/cars/mini-car how can i get 'mini-car' car type

i've found the way to get the id from url. that is;

$url = get_post_permalink(); //Get the url of the current post
 $substring_start_pos = strpos($url, 'pid=') + 4; //Find the position in $url string where the url number starts
 $url_number = substr($url, $substring_start_pos); //Extract the substring form the start position to the end of the url string
 $key_1_values = get_post_meta($url_number, '_song_name', true );
 echo "Your song is called $key_1_values"; 

i want to know how can i do with custom url (to get post slug of custom post type sent from previous page) like i mentioned as example

  • $post->post_name doesn't work? are you outside the loop or inside the loop? If outside you can use get_post_field( 'post_name', get_post() );
    – Orlando P.
    May 9 '18 at 19:16
  • 1
    i am outside the loop. actually i want to display the list of that certain post type i am getting from url.
    – Asad Ullah
    May 9 '18 at 19:55

You can use get_queried_object() which holds the current queried object from where you can get the page slug which is held by the post_name property.

You can use as:

if ( is_page() )
    // // Get the page slug using queried object
    $my_slug = get_queried_object()->post_name;

Hope this will help you.


it is stored in the post_name field in database. you can utilize the following code if you are inside a loop:

$the_slug = get_post_field( 'post_name' );

or if you are outside the loop:

$the_slug = get_post_field( 'post_name', $post_id);

where $post_id is the id of the post you want to retrieve from.

  • I don't think you need the get_post() - just get_post_field( 'post_name' ) should do.
    – Rup
    Jun 5 at 14:49
  • 1
    @Rup thanks, you are right, we don't actually need it if we are inside the loop, corrected..
    – Madi s
    Jun 6 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.