Exclude SlideDeck posts in WordPress

Tags: , , , ,

On a WordPress site I’ve been working on I’m using SlideDeck as a feature article slider on the homepage.  The only problem was I have additional articles populating underneath it and the articles from the SlideDeck were showing up down there as well.

We didn’t want that behavior so I researched how to filter out SlideDeck posts from the post loop.  At this time, it’s apparently easy to filter for content that has a custom meta-tag, but you can’t easily filter for content that doesn’t have a custom meta-tag applied to it.  It’s because when a post doesn’t have a custom met-tag attached to it that custom meta-tag doesn’t come up as “null” or “0”  it simply doesn’t come up at all.  So there’s no way to use query_posts to filter out SlideDeck posts, you have to make custom database query and use $wpdb->get_results.

This post from SlideDeck’s customer support website did most of the heavy lifting for me in putting together the exact query I needed to filter out only SlideDeck posts.  The only problem was that this gave me no insight as to how I could paginate the posts.  Thankfully google came to rescue once again and I found this post and this post which helped me piece together the script.  I posted my findings on SlideDeck’s customer service website, but I’m posted it here as well so I can easily find it again if I need it.

So for the code, inside loop.php before have_posts() I put…

if(is_home()) {
    //query_posts(array( 'meta_key' => 'slidedeck_post_featured') );
    global $wpdb;
    $ppp = intval(get_query_var('posts_per_page'));
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $offset = ($paged-1) * $ppp;
    $sql = "SELECT p.*
        FROM {$wpdb->posts} AS p
        LEFT JOIN {$wpdb->postmeta} AS pm
        ON pm.post_id = p.ID AND pm.meta_key = %s
        WHERE p.post_status = %s AND p.post_type = %s AND pm.meta_value IS NULL
	ORDER BY p.post_date desc
        LIMIT $offset, $ppp";
    $posts = $wpdb->get_results($wpdb->prepare( $sql, '_slidedeck_post_featured', 'publish', 'post' ));

if(is_home())  will make it so it will only filter out SlideDeck posts on the homepage, and apparently the have_posts() works exactly as it normally does even when I change the loop using $wpdb->get_results, which means I don’t have to do much to the rest of the page in order for this to work.  WordPress, you rock <3

This will paginate the posts according to the number of items I have set in the WordPress settings, but for some reason when I did this on the last page it printed out empty posts to fill out that page.

For instance, say I only have 7 posts left on the last page, but I have it set to show a maximum of 10 posts per page – it would print out 3 empty posts on that page.

To combat this I put the following code inside have_posts() so it wouldn’t print anything if the post ID was null.

if(get_the_ID() > 0) {
    //html for posts

Pretty straight forward and everything has been working great so far :)






Permalink » No comments