I running WordPress on IIS & SQL server
. As some of the plugins do not work correctly with DB ABSTRACTION I am using a lot of functions.php
filters as so on.
To make my search results include tags I have added the following code to my functions.php
– http://pastebin.com/BZG20McY
My search results now include tags, but I get plenty of duplicated posts.
Is there any way to filter WP QUERY
and remove all duplicated post before going into the WHILE LOOP
?
Here is my basic loop:
<?php
$query_args = explode("&", $query_string);
$search_query = array('');
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
}
$search = new WP_Query($search_query);
?>
<?php if ($search->have_posts()) : ?>
<?php while ($search->have_posts()) : $search->the_post(); ?>
// SOME POSTS
<?php endwhile; ?>
<?php else : ?>
// NO POSTS
<?php endif; ?>
Any suggestions much appreciated.
Adding this inside the
WHILE LOOP
will solve the problem.It will skip the post in the loop if this post ID is present in our array (meaning, same post went through the loop before).
First simply check if we got a search query. Do this inside the
pre_get_posts
filter to use conditional tags. If we got a search, attach the filter.Then just use the
posts_distinct
or theposts_clauses
filter. Remove the filter im. to not conflict with other queries.