1

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!

1 Answer 1

3

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 );
4
  • 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, 2011 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? Aug 2, 2011 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, 2011 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, 2012 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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