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
));
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.
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).
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.
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();
The reality of your work is that it can’t great, cheap, and fast – at least not all at the same time.
This idea reminds me of the scene in Apollo 11 where NASA engineers are faced with the challenge of fitting a square peg in a round hole:
Great work is expensive because you have to pay for quality with one of those two resources no one wants to part with: time or money. It doesn’t matter which you pick, it’s going to be expensive if you want it to be great.