Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Post Query WordPress

  • SOLVED

Hi There,

I've built some functionality into a custom theme that allows users to exclude certain categories from the blog, archives, search, RSS, etc. This function modifies the post query to exclude the categories that are predefines by the user. Everything is working as expected. I've added the code below to my functions file. (please note that the variable storing the categories holds a value that looks like this: -1,-2,-3,)


function wploop_exclude($query) {
$exclude = get_option('ka_blogexclude');
if ($query) {
$query->set('cat',''.$exclude.'');
}
return $query;
}
add_filter('pre_get_posts','wploop_exclude');



The Problem:
I have some more custom functionality that allows the user to choose a category to pull posts from, and it displays those posts in a nice jquery slider on one of the page templates. The issue I'm having is that the modified query above is also affecting this page template. Is there anyway to define another 'custom query' that will only be used in this particular page template? This new query should only pull posts from the defined category. I'm assuming it will look very similar to the query above, but will be grabbing a different value for the categories. I tried using the is_page_template function but didn't get the result I was after.

I greatly appreciate your help.

Answers (2)

2011-01-26

Gabriel Reguly answers:

Hi,

Or have you tried to use

remove_filter('pre_get_posts','wploop_exclude');

at your particular page template?

Regards,
Gabriel Reguly


WP Answers comments:

Thank you!

Works like a charm.

It's great learning about new Wordpress functions I've never used. ;)

Have a great day.

2011-01-26

Dan | gteh answers:

have you tried adding
wp_reset_query();

above the loop on the page where you do NOT want this function to be applied to?

http://codex.wordpress.org/Function_Reference/wp_reset_query


WP Answers comments:

Thanks for your response.

Yes, I added the reset but it had no effect.