Blog

Wordpress - Related Posts Script

At my job, I currently have three Wordpress sites under my watchful eye. On one, I was tasked with creating a related posts feature. Now, there are certainly enough plug-ins that will do just that, but none of them worked how I wanted to or were generally more complex than necessary. So, I whipped up this little script:

Code: php
  1. function relatedPosts($id) {
  2. /* Build a query to get a list of all posts that share similar tags */
  3. $s = microtime(true);
  4. $q = "SELECT count(*) AS total, p.ID, p.post_title, p.post_date FROM wp_posts p INNER JOIN wp_term_relationships t ON p.ID=t.object_id AND t.term_taxonomy_id in (SELECT s.term_taxonomy_id FROM wp_term_relationships s WHERE s.object_id=$id) AND t.object_id != $id AND p.post_status='publish' GROUP BY p.id ORDER BY total DESC, p.post_date DESC LIMIT 5";
  5. /* Format the output */
  6. $out = "";
  7. $result = @mysql_query($q);
  8. if ($result && @mysql_num_rows($result) > 0) {
  9. $out = "<h3 class=\"related\">Related Posts</h3><ul class=\"related\">";
  10. while ($row = mysql_fetch_object($result)) {
  11. $perma = get_permalink($row->ID);
  12. $out .= '<li><a href="'.$perma.'">'.$row->post_title.'</a> - '.date("F j, Y", strtotime($row->post_date)).'</li>';
  13. }
  14. $out .= "</ul>";
  15. }
  16. echo($out);
  17. }
  18. ?>

Just drop that in the wp-content/yourtheme/functions.php file, where yourtheme is the directory of your active theme. Then add the following in your wp-content/yourtheme/single.php where you want the related posts to appear:

Code: php
  1. relatedPosts(get_the_ID());
  2. ?>

You should now have a list of posts with similar tags. Styling is simple: one h3 and one ul, both with the class "related".

Enjoy!

ID3Lib - A tag library for all seasons

image

ID3Lib [ Download ]

As one may have derived from previous posts, I have a project the uses MP3s quite heavily. As such, retrieving tag data from these is quite an important thing. Having recently migrated my personal server to Windows, I needed a non-Linux variant of my previous tag read. I thought that while I was at it I could add an additional feature, namely saving embedded album art. Unfortunately, I was unable to find any clear directions on how to do so using TagLib, so I created my own library. Three different times. In three different languages. A C++ version, a .NET version and, finally, a PHP version.

Read More

Getting a YouTube FLV through PHP

PHP Corner

Recently, while making some additions to the Music Page, I came across the need to get the location of a YouTube FLV. Unfortunately, most of my googling turned up outdated results that no longer work. Fortunately, unlike a certain other site, it's not impossible. Below is a quick explanation of what I got to work. I'll be assuming that you know how to get your hands on the video ID (the "v" parameter in the query string).

Read More

A tiny PHP/Twitter script just for you!

Was rather bored this afternoon and then I though to myself "Why not add Twitter functionality to my music page?" So, after some digging through Twitter APIs and some refreshers on HTTP Authentication, I wrote this tiny, beautiful little script.

Code: php
  1. // Return values
  2. define ("TWITTER_OK", true);
  3. define ("TWITTER_UNAUTHORIZED", -1);
  4. define ("TWITTER_TOO_LONG", -2);
  5. define ("TWITTER_COULD_NOT_CONNECT", -3);
  6. function sendTweet ($status, $userName, $userPass)
  7. {
  8. // Open a socket to Twitter
  9. if (!($socket = fsockopen ("twitter.com", 80)))
  10. return TWITTER_COULD_NOT_CONNECT;
  11. // Create the headers
  12. $headers = "POST /statuses/update.xml HTTP/1.1\r\n";
  13. $headers .= "Host: twitter.com\r\n";
  14. $headers .= "Authorization: Basic ".base64_encode ($userName.":".$userPass)."\r\n";
  15. $headers .= "Content-length: ".strlen ("status=$status")."\r\n\r\n";
  16. $headers .= "status=$status\r\n";
  17. // Send the headers
  18. fwrite ($socket, $headers);
  19. // Get the response
  20. $response = "";
  21. while (!feof ($socket))
  22. $response .= fgets ($socket, 1024);
  23. // Check for HTTP status OK (200)
  24. if (!strpos ($response, "200"))
  25. return TWITTER_UNAUTHORIZED;
  26. return TWITTER_OK;
  27. }
  28. ?>

It is as simple as it looks. Enjoy!