Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

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:

$custom_query = array();
$custom_query['tax_query'][] = array( 'taxonomy' => 'media', 'terms' => array('media'), 'field' => 'slug', 'operator' => 'NOT IN' );

$args = array_merge( $wp_query->query, $custom_query );
query_posts( $args );
share|improve this answer
    
Thanks a million, Seagyn. Your assumptions were correct, but the solution you proposed didn't work, unfortunately. Here's a Pastebin of the code I put in: pastebin.com/huHDMpL6. The modifications to search results are still being ignored. –  Egor Aug 2 '11 at 15:17
    
Do you have an example of this? In the code you are showing me the taxonomy has changed to "image" now. Are you looking at leaving out posts that have images? –  Seagyn Davis Aug 2 '11 at 16:48
    
Only a local example. 'Media' is a non-hierarchical taxonomy, I want to be able to take a post and tag it according to media used - images, video, audio, etc. The 'media' term was a typo. Thanks! –  Egor Aug 2 '11 at 17:43
    
I've been looking for a solution to this same problem for a while. This answer provides not only a simple but also flawless method to accomplish what's being asked, and should be accepted as the correct answer. +1 –  Cthulhu Oct 16 '12 at 10:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.