I have a custom php page where I need to display details of a WordPress post based on the URL entered from the Admin. In the Admin, when adding the project detail, we enter few URLs of article post which are linked to this project in a text box. The URLs are stored as comma separated values in the database. Through explode I'm able to fetch all of the URLs but I'm not able to get the details of the post based on the URL.
<?php
$mark=explode(',', $rowPr['post_url']);//what will do here
foreach($mark as $out) {
$out;
}
?>
So, I'm looking for the custom SQL query outside of WordPress to fetch the post detail based on the URL of the post. I will require the post title, post date, post author etc.
I have a custom php page where I need to display details of a WordPress post based on the URL entered from the Admin. In the Admin, when adding the project detail, we enter few URLs of article post which are linked to this project in a text box. The URLs are stored as comma separated values in the database. Through explode I'm able to fetch all of the URLs but I'm not able to get the details of the post based on the URL.
<?php
$mark=explode(',', $rowPr['post_url']);//what will do here
foreach($mark as $out) {
$out;
}
?>
So, I'm looking for the custom SQL query outside of WordPress to fetch the post detail based on the URL of the post. I will require the post title, post date, post author etc.
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
.