How to get post detail based on URL of the post

admin2025-01-07  3

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.

Share Improve this question asked May 1, 2018 at 17:41 Rakesh OjhaRakesh Ojha 237 bronze badges 4
  • 1 The queries would run faster if instead of adding URLs as postmeta, you added the post IDs. You could have your custom metabox list all the posts (or pages, or whatever CPT you are using) and allow admins to select them from the list, then save the ID as the actual meta and query by post ID. Or, if the editors are a little more tech savvy, you could let them manually enter the post IDs (copy and paste style). – WebElaine Commented May 1, 2018 at 18:37
  • Yes, adding IDs would have been easier, however, finding the IDs of the post will require extra efforts. Editors aren't tech savvy and thus the requirement to just copy and paste the URLs of the posts. – Rakesh Ojha Commented May 2, 2018 at 16:21
  • I would use Advanced Custom Fields to add a field that allows users to select the posts, but behind the scenes, it will store the IDs. – NightHawk Commented May 2, 2018 at 18:22
  • This is a separate PHP module from WordPress and this I'm not sure if we can use ACF there. From the Project module Admin, a linked post from WordPress are entered as related linked to a project. – Rakesh Ojha Commented May 3, 2018 at 2:56
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253419a122.html

最新回复(0)