0

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.

  • 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 May 1 '18 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 May 2 '18 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 May 2 '18 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 May 3 '18 at 2:56
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.

  • The URLs do not have dates in the URLs, so please suggest the preg_match keeping this in mind. Also, since we are writing this custom code outside of the WordPress, will the get_page_by_path () work in php file? – Rakesh Ojha May 2 '18 at 16:24
  • I added the above code in my code however it didn't return/echo any value. Below is the code: $mark=explode(',', $rowPr['other_papers']); foreach($mark as $out) { $out; $components = parse_url($url); preg_match('^/\d*/\d*/(.*)/$', $components['path'], $matches); echo $matches[1]; } – Rakesh Ojha May 2 '18 at 16:48
  • get_page_by_path() is a WP function so no, it will not work in straight PHP. You could either include WP header (often overkill and awkward) or instead pull posts using the WP REST API. Using a regex as in this answer, adjusted to your unique permalinks, you can then grab each post (or Page, or CPT) by slug: wordpress.stackexchange.com/questions/257505/… – WebElaine May 2 '18 at 17:16
  • If you're working in WordPress, with WordPress data, why would a script be running outside of the WordPress context? Didn't you say that you need this to look up more WordPress data, in which case you would then need the WordPress context? – NightHawk May 2 '18 at 18:23
  • This Project module is separate from WordPress but it needs to show posts from the WordPress website which are related to a Project and thus we need to fetch post details based on the post URL entered as linked post. I tried to use include () to load WordPress environment, but it stops the PHP page and the page becomes blank. – Rakesh Ojha May 3 '18 at 2:52

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.