I am trying to modify (refine?) Worpress search results by using WP_Query, just as it’s explained in the Codex. The snippet featured in that article is on the top of the Search Template PHP file, and all results are displaying accordingly.
When I try to append the WP_Query array with arguments of my choice, to create a modified query (for example: posts containing term user searches for, not included in a certain taxonomy), WP_Query completely ignores the appended arguments and gives me the original search results all over again.
Code below:
$search = new WP_Query(array( 'taxonomy' => 'media', 'field' => 'image', 'operator' => 'NOT IN'));
I also tried it this way, but I have a feeling it’s completely wrong:
<?php
global $query_string;
$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]);
} // foreach
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'media',
'field' => 'slug',
'terms' => 'media',
'operator' => 'NOT IN'
)
),
$search_query
);
What follows is the usual:
if ( $search->have_posts() ) :
while ($search->have_posts() ) : $search->the_post(); ?>
I am not very fluent when it comes to coding, but trying to teach myself the quirks of WordPress as a CMS. I read through replies on this site, none of the methods used did it for me, though.
Can you see what I’m doing wrong? Thanks!
You need to just merge the current query for the search with the part you want to add which you came close to doing already.
I assume you have a custom taxonomy called media and you specifically want to leave out posts with the term media (like a term found in a tag or category) in the search results
In your case: