Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to get post detail based on URL of the post

I agree with WebElaine’s comment, that storing IDs would be far more reliable, since URLs can so easily change.

That said, if you have a URL like so:

http://domain.com/2018/01/this-is-my-post/

You can use something like this to obtain the post name:

$url="http://domain.com/2018/05/this-is-my-post/";
$components = parse_url($url);
preg_match('`^/\d*/\d*/(.*)/$`', $components['path'], $matches);
echo $matches[1];

Using the post name in $matches[1] you can use get_page_by_path to obtain the post object.

$post = get_page_by_path($matches[1]);

From there you can retrieve any value with its ID using $post->ID.

Related Posts:

  1. Can the Next/Prev Post links be ordered by menu order or by a meta key?
  2. Advanced search form with filters for custom taxonomies and custom fields
  3. Meta query with boolean true/false value
  4. Custom field values deleted when trashing custom post type
  5. Custom Post type – how to get checkbox to update meta field to null
  6. How do I Paginate Search Results for Custom Post Types?
  7. How do I remove all the metaboxes for a custom post type?
  8. Custom Queries: Joining On Meta Values From Two Custom Post Types
  9. How to programmatically create a connection with [Plugin: Posts 2 Posts] on cpt publish?
  10. Grossly inefficient wordpress loops!
  11. How Do I Use WP_Query to Run This Database Query as Search Result?
  12. WordPress Admin Panel search posts with custom post meta values along with title
  13. Rest Api v2 orderby meta_key in custom post type
  14. bulk Update post_meta with default values
  15. Custom post type category not displaying in custom post type
  16. How to make custom post meta wp_editor translatable?
  17. How to add multiple featured image in meta box in post editor?
  18. update_post_meta() whenever custom post type is updated
  19. Add Post Url to Manage Edit Screen
  20. How to best delete orphan wp_postmeta
  21. Display all custom post type posts and order them by an optional meta_key
  22. How can I get_post_meta() in the most efficient way?
  23. need advice on how to do a lists using custom post types – taxonomy vs postmeta
  24. Count posts with specific term_meta
  25. Custom search for custom post meta with pre_get_posts interferes with WP search
  26. WordPress Orderby Numeric Value Not Working
  27. Replace title column in post list with post meta value
  28. My theme saves their custom post type’s metadata as a serialized array, how to access the keys?
  29. List all images from a single post meta value
  30. Ordering posts in Search & taxonomy by post_meta
  31. how to delete all users and posts based on ‘user_meta’?
  32. Meta Key Value in current-user-only loop
  33. How to set YouTube video as featured image?
  34. How to implement a Google map store locator
  35. Widget: Custom Post Type Post Listing Dropdown on Admin Side
  36. changing meta value and meta key of price field
  37. Posting to a Custom Post Type from front end – user generated content
  38. Update Post Meta for a logged in user
  39. Restrict Access to Posts based on Custom User and Post Meta Data
  40. get_post_meta returning no value in custom post type alert email
  41. WordPress request fiter order by related post’s post_title
  42. Getting thumbnails of a custom posts featured images in a page-templates metabox
  43. Custom fields for custom post type
  44. Show future events based on custom field AND order by date
  45. If Post Meta Equals Current Title Conditional Advanced Custom Fields
  46. create custom meta box with default value
  47. How to Echo Metadata Value in Currency Format
  48. Issue on Working with Metabox – Checkbox to enable Sale Items
  49. delete_post_meta() for whole CPT / multiple posts?
  50. How to move a post to different post type with all meta data?
  51. Why do I have to press the “Update” button twice to save my meta box values?
  52. how to show records that don’t have custom meta value
  53. WP API Response does not show my registered metadata
  54. How to detect that the save_post hook is calling the callback associated to the current edit post page only
  55. get_post_meta returns NULL in front-end, but correct result in back-end
  56. Problem with adding custom post type meta to header by plugin
  57. Meta box data is saved but NOT displayed in the meta box text field. Why?
  58. How to call a post’s metadata in shortcode.php?
  59. Echo custom post meta from options array
  60. Value of post meta dropdown is not showing in WordPress
  61. WP_Meta_Query object with conditionals
  62. Trying to write shortcode with get_post_meta but isn’t working
  63. HM CMB: Post Select Field for CPT ID
  64. updating one custom meta field only
  65. Add a class to post_class if more than one post shares same meta_value_num
  66. Problem Saving Custom Post Type Meta Values
  67. Should I use a custom taxonomy or custom post type for grouping a list of panels associated with a group of tests?
  68. Add a form in every post and save data in post meta
  69. Show posts from WP Custom Post Type selected from a field in a metabox
  70. Creating an archive page or simple template to list all values of a custom field of specific post type listing
  71. Peepso: Add extra data field (custom post meta) to postbox
  72. Change picture attachment meta of all occurrences of a picture
  73. Unknown Post Meta Records
  74. Get Previous Post based on Custom Field Name, for Custom Post Type
  75. Saving custom post types post_meta over REST-API fails
  76. add action save post type and update post meta woocommerce
  77. get_post_meta not working with variable as a post_id for dynamically get the postid
  78. Linking posts together with Advanced Custom Fields “both ways”
  79. Order posts by meta value hiding posts instead of re-ordering
  80. custom search results – order results differently by post type
  81. Stripe multiple transactions
  82. How to get Current week and current date record wp query
  83. get_post_meta for Custom Post Type ( CPT )
  84. Edit is changing my custom’s post type parent id
  85. How to add post reference field to a plugin?
  86. WordPress custom post type
  87. Edit post meta direct from post.php?
  88. Custom Post Type meta oembed html output resulting in WSoD
  89. Timetable of Custom Meta Data using Custom Post Type and Custom Taxonomy
  90. Change the contents of a dropdown through the admin panel?
  91. fetch meta fields inside a widget
  92. Custom post type – update_meta not working
  93. Custom Form Data save after Login User
  94. Use a Variable in update_post_meta as the $meta_key
  95. Changing to different post type maintains the redirection?
  96. Post Meta for Custom Post Types
  97. How do I add the “orange counter” next to a Custom Post Type?
  98. getEntityRecords/useEntityRecords: How to use CPT metadata?
  99. CPT meta values intermittently return empty
  100. Quering a post object based on another related post object
Categories custom-post-types Tags custom-post-types, post-meta
not able to access $_POST on backend profile update
Is it possible to make a custom post that adds multiple pages?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress