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.

Probly easy question but just cant seem to find whats wrong. I cant build a query based on a search when i use ' ' but not when i use a alrdy declared variable.

This works

$query2 = new WP_Query( 's='motorkap met' );
 while( $query2->have_posts() ) {
            $query2->the_post();
echo the_title();?> </br> <?php
}

This doesnt for some reason.... I cant seem to find how to pass the already declared variable

$zoekterm = removeCommonWords('motorkap met'); 

$query2 = new WP_Query( 's=$zoekterm' );
 while( $query2->have_posts() ) {
            $query2->the_post();
echo the_title();?> </br> 
}

Very stupid I know but been searching for over 2 hours alrdy :( must be passing it wrong somehow tried everything from echo, to just $zoekterm to 's='$zoekterm' ' etc...

share|improve this question
    
Did you try "s=$zoekterm" too? Note the double quotes. –  toscho Jul 12 '12 at 18:25
    
just tried didnt work –  Bram Jul 12 '12 at 18:32

1 Answer 1

up vote 1 down vote accepted

Assuming removeCommonWords() returns a string, you can do the following:

$zoekterm = removeCommonWords('motorkap met'); 

$query2 = new WP_Query( 
    array(
        's' => $zoekterm
    ) 
);

while( $query2->have_posts() ) {
    $query2->the_post();
    echo the_title() . '</br> ';
}

Sometimes the array notating is more useful than the string notation ...

share|improve this answer
    
thanks a lot, this works could have taken me ages to find... Fast question now that I got your attention how do I set it that it only searches the title and not content. If thats alright to ask, don't want to be a gimme the code guy. Ill go on google search hunt if needed. Very happy that this works alrdy. –  Bram Jul 12 '12 at 18:50

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.