Sitewide Recent Posts – hack

I started using WordPress Mu for a couple of sites that I am helping out with. The key to WordPress Mu is the ability to have multiple WordPress sites under the control of a main WordPress site. For example, if you have a common topic, you can have multiple sub-sites under the main site. Examples of these are the two sites I am helping out with 9/12 Candidates and NC Freedom. While 912 Candidates has sub-sites based on each state, NC Freedom uses sub-sites based on regions within the state.

Another part of WordPress Mu is the ability to have a sitewide plug-in. A sitewide plugin can pull information throughout the site. On 912 Candidates I create a sitewide plug-in to handle the Gold List of candidates. It allows each sub-site to enter in their candidate and have the Gold Lists update across the site accordingly.

For the NC Freedom site, I needed a way to show the most recent post from across the site. Luckily, WordPress Mu has a site with plug-ins created for sitewide use. On this site I found AHP Sitewide Recent Posts. Unfortunately there was a slight issue with how the recent posts were returned.

The 0.6.1 version would get each blog based on the update date value and then get each post for those blogs, posting the excerpts as it went through each blog. This meant, that if a blog was updated for any reason, it might have an early update date and it’s posts would show as being newer than other posts that were in reality posted more recently. The best option would be to put all of the posts into an array, then sort the array on the post date and then finally display them in the appropriate order.

The first change I made was to add an array variable and a counter.

1
2
$postArray = array();
$i = 0;

Next I shortened the loop area to only include the blog and post queries and not display any output. This also included separating out the sql statement for the comment query, since I would be using this twice. First to loop through each comment and then second to insert that comment into the array. The “$i” counter allows each post to be inserted at the next array spot. I also modified the query to put the post date as the first column and the second column as the blog id ($blog AS this_blog). These changes will help with the sorting and permalink structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
foreach ($blogs as $blog) {
 
            // we need _posts, _comments, and _options tables for this to work
            $blogPostsTable = "wp_".$blog."_posts";...
 
            // fetch the ID, post title, post content, post date, and user's email for the latest post
            $sql = "SELECT $blogPostsTable.post_date, $blog AS this_blog, $blogPostsTable.ID, $blogPostsTable.post_title,
                $blogPostsTable.post_content, wp_users.display_name,
                wp_users.user_email, wp_users.user_login
                FROM $blogPostsTable, wp_users
                WHERE wp_users.ID = $blogPostsTable.post_author
                AND post_status = 'publish' AND post_type = 'post'
                AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
                AND $blogPostsTable.id > 1
                ORDER BY $blogPostsTable.post_date DESC limit 0,1";
            $thispost = $wpdb->get_results($sql);
 
 
            // if it is found put it into the Array
            if($thispost) {
 
                $postArray[$i] = $wpdb->get_row($sql, ARRAY_A);
                $i++;
            }
        }

Finally, after the array is done, there is a check to see if there are actual rows in the array. If there are rows, then the array is sorted by the first column, which is the post date. Then the script loops through the array to display each posts.

1
2
3
if(count($postArray) > 0){
    array_multisort($postArray, SORT_DESC);
    for ($a = 0; $a < count($postArray); $a++){

When each row is called, a little different syntax is required. There has to be a reference to the array item, then the column.

1
$postArray[$a]['post_date']

Here is the complete modified script. The displaying of the post has been modified to fit better with the layout of site I am using it on. Look here for the working example.

About DeanLogic
Dean has been playing around with programming ever since his family got an IBM PC back in the early 80's. Things have changed since BASICA and Dean has dabbled in HTML, JavaScript, Action Script, Flex, Flash, PHP, C#, C++, J2ME and SQL. On this site Dean likes to share his adventures in coding. And since programming isn't enough of a time killer, Dean has also picked up the hobby of short film creation.

About DeanLogic

Dean has been playing around with programming ever since his family got an IBM PC back in the early 80's. Things have changed since BASICA and Dean has dabbled in HTML, JavaScript, Action Script, Flex, Flash, PHP, C#, C++, J2ME and SQL. On this site Dean likes to share his adventures in coding. And since programming isn't enough of a time killer, Dean has also picked up the hobby of short film creation.

Leave a Reply

Your email address will not be published. Required fields are marked *

*