by Joshua Kelly on Jun 28th, 2013 under Uncategorized with No Comments

Book it!

How to stop post duplication when using multiple WP_Query() requests

by Cailen on Feb 12th, 2012 under Tutorial and WordPress with 2 Comments

On my site SalaciousSound I have two selections of articles: five ‘featured’ posts which appear at the top of my index, above the fold, and ten more of my most recent articles.

I wanted to exclude the featured articles from appearing in the regular articles section, and I did so by using the WP_Query() parameter ‘post__not_in’.

The reason for writing this article is that collecting the post IDs from my first WP_Query and passing them to the second was, for me, non-trivial because the five featured articles appear in my header.php file and the ten regular articles appear in index.php.

In order to achieve my desired result I had to set a php variable in my functions.php file, and then declare it as a global variable in my header.php file and index.php file.

Here’s how the relevant code looks:

from functions.php
$featIDs = array();
from header.php
global $featIDs;
$feats_query = new WP_Query('category_name=Features&posts_per_page=5');
while ($feats_query->have_posts()) : $feats_query->the_post();
	array_push($featIDs, $post->ID);
from index.php
global $featIDs;
$index_query = new WP_Query(array( 
	'posts_per_page' => 10, 
	'post__not_in' => $featIDs

How to use WP_Query() with pagination

by Cailen on Feb 12th, 2012 under Tutorial and WordPress with No Comments

It turns out using WP_Query() AND paginating your results properly is not entirely straightforward. The WordPress Codex documentation and information on the help forums is also pretty bad.

This article explains how to make custom queries using WP_Query() AND still allow for pagination.

My use case is: a custom query on my index page which excludes certain posts, and wp_pagenavi for paginating my archives.

I explain in detail why I am using WP_Query() instead of the regular WordPress loop in my next article ‘How to stop post duplication when using multiple WP_Query() requests‘.

In short, WP_Query doesn’t know what page you’re on like the regular WordPress loop does – you need to tell it which page of posts to ask for (and, in addition, how many posts per page you want it to retrieve).

Here’s the relevant code:

$pagedNum = get_query_var('paged');
$index_query = new WP_Query(
		'posts_per_page' => 10, 
		'paged' => $pagedNum

Add TinyMCE WYSIWYG Editor and HTML Capabilities to WordPress User Bios

by Cailen on Sep 13th, 2011 under Resources and WordPress with 2 Comments

I’m at the end of another project here and as usual I’ve had to modify the WordPress core slightly to give my client some CMS functionality that vanilla WordPress lacks. Specifically, by default you are not able to add any html to the ‘Biographical Info’ field on user profile, much less use the WYSIWYG editor bundled with the post editor on that field – any html is automatically stripped out when you ‘Update Profile’.

So there are two ways that you can achieve what I wanted to do, and both utilize the functions.php file.

Strategy One – just allow for html (no TinyMCE)
Strategy Two – add TinyMCE to the User ‘Biographical Info’ field

Use the above code AND the code below

function biographical_info_tinymce() {
	if ( basename($_SERVER['PHP_SELF']) == 'profile.php' || basename($_SERVER['PHP_SELF']) == 'user-edit.php' && function_exists('wp_tiny_mce') ) {
		add_filter( 'teeny_mce_before_init', create_function( '$a', '
			$a["theme"] = "advanced";
			$a["skin"] = "wp_theme";
			$a["height"] = "300";
			$a["width"] = "440";
			$a["onpageload"] = "";
			$a["mode"] = "exact";
			$a["elements"] = "description";
			$a["theme_advanced_buttons1"] = "formatselect, bold, italic, pastetext, pasteword, bullist, numlist, link, unlink, outdent, indent, charmap, removeformat, spellchecker, fullscreen, wp_help";
			$a["theme_advanced_blockformats"] = "p,h2,h3,h4,h5,h6";
			$a["theme_advanced_disable"] = "strikethrough,underline,forecolor,justifyfull";
			return $a;' ) );
		wp_tiny_mce( true );
add_action('admin_head', 'biographical_info_tinymce');

WPAudio WordPress Plugin Crashes Safari Browser When Song is Played From Post

by Cailen on Jul 3rd, 2011 under Tutorial and WordPress with No Comments

I encountered a problem on my music blog SalaciousSound today. I use a plugin called wpaudio, which uses javascript and the soundmanager2 library (with flash fallback) to play mp3 audio files. I was finding that, on a particular post, whenever I played an mp3 the browser would crash. The plugin was working fine on all other posts, so I knew that this had something to do with the number of mp3 files on this particular post.

How to reproduce the crash

Create a post with more than 20+ mp3s to play, or go to a page (archive for example) where more than 20+ players are present – it doesn’t matter if they’re all inline or each have their own wpaudio markup, you just need more than twenty players. The crash was occurring in Safari. Chrome was working ok. I didn’t test Firefox or Internet Explorer.

I have a monthly series that I run called the Monthly Best of the Blogosphere, wherein I post 25-30 tracks that have been really popular that month. Only on these posts was the browser crash occurring.

How to fix the crash

You have to edit the wpaudio.js (or wpaudio.min.js file, depending on what you use – I use the minified wpaudio.min.js file myself). You have to change two lines of code in this file, and add an additional one. It’s pretty easy, and if you follow my instructions carefully you can do this with minimal coding knowledge.

Find this function:

function WpaudioHTML5 (parent) {

Find these two lines – they are sequential:

start = player.seekable.start();
end = player.seekable.end();

Change them to this:

start = player.buffered.start();
end = player.buffered.end();

Next, find these two lines:

player.src = parent.getUrl();
player.volume = 1;

And add this line after them:

player.preload = "none";

Voila! Problem solved!