WordPress & The Web's Golden Bounty

I thought I'd share this, in case anyone wants to duplicate it.

Programming stuff follows...

Over on the last host and the last WordPress installation, I used a WordPress plugin called "miniposts" to generate that "Web's Golden Bounty" list over on the top right of this blog. It creates a WordPress 'widget' and it lets you mark posts as "miniposts"; when the blog is rendered, miniposts go into the minipost widget (which I titled "The Web's Golden Bounty") and not into the main post stream. It's a terrifically handy way of dropping links into the blog without having to bother saying anything about them. And it's an idea I stole from the venerable Making Light blog.

Well, something about this new WP installation - even though it's the same version of the plugin and the same version of WordPress - broke the minipost plugin. It stopped remembering its settings, and was apparently messing with the overall WordPress settings system, somehow. I tried to figure out how to fix it, but success eluded me. And, the plugin author hasn't updated it in quite a while, and there doesn't seem to be anyone else maintaining it. It appears to be dead.

How to duplicate what it did...

My solution was to change the way WP handles "aside" posts. These are small posts that are formatted somewhat differently from normal posts. They're supposed to be kindof like the miniposts - one-liners, quick links, etc. - but they don't show up on the sidebar, they show up in the main post stream, looking all ugly and cluttery. I have no use for such things.

Here's what I did:

1. Make a WordPress 'child theme'. I'd already done this, to customize the default WP theme. You can do that a bunch of ways. There's even a plugin to help. Mine's called "Sweet Child O' Mine", of course.

2. To that child theme, along with my cosmetic changes, I added a file called 'functions.php'. And in that file, I put the following code:

<?php

// Hide asides from the front page. We're going to show them in the PHP code widget
function exclude_asides( &$wp_query ) 
{
    // don't hide them in an admin page (or you'll never be able to edit them!)	
    if (! is_admin() )
    {
        if ( $wp_query->is_main_query() && ! $wp_query->is_singular() ) 
        {
		// Array of post formats to exclude, by slug,
		// e.g. "post-format-{format}"
		$post_formats_to_exclude = array(
			'post-format-aside'
		);
		
		// Extra query to hack onto the $wp_query object:
		$extra_tax_query = array(
			'taxonomy' => 'post_format',
			'field' => 'slug',
			'terms' => $post_formats_to_exclude,
			'operator' => 'NOT IN'
		);
		
		$tax_query = $wp_query->get( 'tax_query' );
		if ( is_array( $tax_query ) ) {
			$tax_query = $tax_query + $extra_tax_query;
		} else {
			$tax_query = array( $extra_tax_query );
		}
		$wp_query->set( 'tax_query', $tax_query );
	}
    }
}

// Call the above hook function before every WordPress query:
add_action( 'pre_get_posts', 'exclude_asides' );
?>

As the comment says, this hides asides from the main post stream. WordPress will read this file and execute it as part of its normal rendering.

3. Now, asides are hidden, but how to get them into the sidebar... Well, in my travels, I found another neat WP widget called the "Executable PHP Widget" which you can add to your sidebar and which acts as container for any PHP code you might want to run. So, all I needed to do was to get it to read the last N aside posts and put them in a list in the widget's output.

And that's not very hard:

<ul id="elsewhere">
<?php

// read the last 5 asides
$args = array(
    'posts_per_page' => 5,
    'post_type'=> 'post',
    'post_status' => 'publish',
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 'post-format-aside' )
        )
    )
);
$myQuery = new WP_Query( $args ); 

// Loop through the posts, add them to our UL
while ( $myQuery->have_posts() ) : $myQuery->the_post();
	echo '<li>' .  get_the_content() . '</li>';
endwhile;
?>
</ul>

4. Asides are still showing up in the "Recent Posts" widget. Googling turns up the "Standard Recent Posts" widget, which works like the Recent Posts widget but only shows standard posts - no aides, images, links, etc..

And that's it.

It would have been nice to have my child theme handle rendering the asides in the toolbar (thus being able to skip the PHP Widget), but this works perfectly well.

Now, to add an entry to The Web's Golden Bounty, I write a one-liner post (typically just a link with a short title), then mark it as an aside instead of a 'standard' post. The aside's text becomes the link, the title is ignored, and, voila: a new minipost is born.

This new system is a lot better than the old if, like me, you create a lot of posts from the 'Press This' tool. The miniposts plugin doesn't have a way to interact with that tool. To make a minipost with the Press This tool, you had to write the post in Press This, save as a draft, then re-edit the in the main editor (where the minipost checkbox lives). But launching the edit window from Press This puts the edit page in the same browser window as the page you were writing about, and takes that page out of the window's history. So you couldn't just use the browser's 'back' button to get back to the page you were commenting on. WTF. To get back to the page you were writing about, you had to write, save draft, re-edit, check the miniposts box, publish, reload ok-cleek's front page, find the new minipost and click it. Hassle. Now: write, set as 'aside', publish, done.

So, it works. But it's not perfect, yet. Links to the asides show up on the prev/next links you see if you're reading an individual post. And clicking a link to an aside will take you to the aside itself, where you could, if you wanted to, leave comments. But I'll fix that as soon as I figure out how to.

2 thoughts on “WordPress & The Web's Golden Bounty

  1. ChrisR

    Very cool! Thanks for taking the time to figure it out. Having done a small amount of web development in PHP, I am not really envious.

    1. cleek Post author

      luckily, most of this was just assembling things that other people have already written. i don’t think i could have done it otherwise

Comments are closed.