wp post filter

I think i’ve found a way to work around the problem though, it’s not as elegant as the previous code, but it doesn’t involve any additional queries, so there’s no downside other then having to use different code..

I don’t use a front page setup, so the code differs for your usage slightly..

function frontpage_query( $query ) {
if( $query->is_front_page ) {
$query->set( 'category__in' , array( 1 ) );
$query->set( 'caller_get_posts' , 0 );
}
return $query;
}
add_filter( 'pre_get_posts' , 'frontpage_query' );

..where as in my testing, the above used..

if( $query->is_home )

.. it might make a difference, so i'm just pointing this out in advance..

Add that code to your theme's functions.php, then get rid of the query_posts and code from before, and just put this inside your loop.. (after the while( have_posts( ... before the HTML etc..)
if( is_front_page() ) {
if( !in_category(1) ) continue;
}

The function first sets the category and sets caller_get_posts to 0, which basically means stickies at the top...

.. the condition then catches other sticky posts that seem to crop up in the results incorrectly, so continue basically instructs the loop to skip that result..

End result - should be posts in set category, with stickies AT THE TOP... 🙂 if all goes to plan... 🙂

Author: Ashif

Hi, my name is Ashif. I've been earning a living online since 2006 and love to share what I've learned. You can connect with me on Facebook, Twitter, YouTube and Google Plus. Don't forget to subscribe to my blog to keep up with my latest tips and tricks. You can also send questions in my mail.

Leave a Reply

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