I've built the following snippet from other post examples that say doing query modification any other way is punishable by great scorn, but it's not working. I'm getting results that include non-published posts and pages which clearly shouldn't happen:
function post_conditions($where)
{
$where .= "AND post_content NOT LIKE '%::exclude tag::%'";
return $where;
}
add_filter('posts_where','post_conditions');
function mysearch($query)
{
$query->set('post_type','post');
$query->set('post_status','publish');
$query->set('posts_per_page',20);
$query->set('paged',get_query_var('paged'));
}
add_action('pre_get_posts','mysearch');
while( have_posts() ){
the_post();
echo get_the_excerpt();
the_tags();
}
if (get_query_var('paged'))
my_paged_function();
wp_reset_query();
The get variables look like so: ?s=mysearchterm&submit=+GO%21+
On my blog template, I'm using the verbotten query_posts() function to achieve the same effect and it works perfectly.
I don't know what's going wrong. Any ideas?
Since this code lives in your template, it's not firing in time to catch the pre_get_posts
hook. By the time the template is chosen / running, wp_query is done setting up, and pre_get_posts
is over.
You need to move this functionality into your functions.php
files, and try and use some other means by which to determine if you want to change the query. There's lots of information available to you - including if it's an archive, a single page, a single post, the post id, and more - hopefully with that information, you can determine if you want to modify the query.