Show Search Results from EITHER Content/Titles OR Tags
-
Hi there. I’ve been working on my page’s searching for a few days now. I’ve finally found some info on how to progress (I had been stuck for a few days), but I’ve run into another problem…
Basically what I want to do is have searches show results (pages and/or posts) generated by the searched term(s) matching EITHER the page’s/post’s title/content (done automatically by WordPress’ default search) OR the post’s tags.
For example, the user types “awesome” and searches for it. The results would show pages/posts that have “awesome” in their title, in their content, OR in their tags.
What I have as my searching method now is this:
$temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('showposts='.$resultsPerPage.'&paged='.$paged.'&orderby='.$sortBy.'&s='.$s); while ($myArray->have_posts()) : $myArray->the_post(); $title = get_the_title(); $content = get_the_content(); echo '<h2>' . $title . '</h2>'; echo $content; endwhile; include (TEMPLATEPATH . '/inc/nav.php' );
$wp_query->query('showposts='.$resultsPerPage.'&paged='.$paged.'&orderby='.$sortBy.'&s='.$s);
^This line^ being what’s doing the searching (if I understand correctly). So it will give me results that contain the search term(s) stored in $s in their title or content, order them by whatever is stored in my $sortBy variable (by default I have it as ‘date’), show how ever many posts according to the $resultsPerPage variable (by default I have it as 11), and paginate the results (if necessary).
NOW… If I include the ‘tag’ parameter in that line:
$wp_query->query('showposts='.$resultsPerPage.'&paged='.$paged.'&orderby='.$sortBy.'&s='.$s.'&tag='.$tags);
where $tags is a string of the search term(s) ($tags = str_replace(” “, “,”, $s);), then it will show results that have the search term(s) in BOTH their title/content AND their tags.
So, for example, the user searches “awesome.” The only results will be posts that have “awesome” in both the title/content AND the tags. If a post has “awesome” in its content but not its tags, it will not show; if a post has “awesome” in its tag but not its content/title, it will not show. Again, I’d like to show results that have “awesome” in EITHER the title/content OR the tags.
At first, I thought just replacing the & with | (or) before ‘tag’ in the query parameters would do it, but apparently that doesn’t seem to work (nor do I think it’s proper code for the arguments).
I’ve also tried having two queries–one that searches for the search term(s) in the title/content with ‘s=’.$s and another that searches for the search term(s) in the tags with ‘tag=’.$tags, combining the two queries, then removing duplicate posts. I’ve tried this a few different ways and haven’t gotten any of them to work…
Is there any way to do what I’m trying to do? The simpler the answer, the better (this is my first time using WordPress and PHP) :P. Also, I’m worried about using something other than $wp_query->query($args); because I was having trouble paginating results when I used other methods such as query_posts or get_posts. I don’t know if that matters in the end, but that’s why I’ve been hesitant to change it.
I hope that all makes sense. Thanks in advance for the help!
- The topic ‘Show Search Results from EITHER Content/Titles OR Tags’ is closed to new replies.