So right now my query looks like this:

$allsearch = &new WP_Query("s=$s&showposts=-1");

I understand what this query does and it works, but not I want to add the ability to query posts by $s OR by multiple authors. I don't exclusively want posts that match the search or posts by the list of authors, but I want both together.

I want to build the argument according to this.

This is what I have so far:

$args = array (
    'author' => '1,2,3,4,5',
    's' => $s,
    'posts_per_page' => -1 

$allsearch = &new WP_Query($args);

However I'm pretty sure that this query only returns posts that or both by the listed authors and match the search key.

All suggestions or guidance is appreciated.

  • Remember that when you post code as exemple make it a valid PHP code before posting. – Bordoni Oct 29 '14 at 3:05

You don't really need to use the &new WP_Query just new WP_Query.

The following code will resolve your problem.

add_filter( 'posts_search', 'q166788_search_modify', 10, 2 );

function q166788_search_modify( $sql, $query ){
    // Only if our variable is true we do the black magic
    if ( ! isset( $query->query_vars['search_conditional_toggle'] ) || $query->query_vars['search_conditional_toggle'] !== true ){
        return $sql;

    return preg_replace('/AND/', 'OR', $sql, 1 );

// The actual search
    $s = 'bord';

    $args = array (
        'author' => '1,2,3,4,5',
        's' => $s,
        'search_conditional_toggle' => true, // Make the black magic happen
        'posts_per_page' => -1

    $allsearch = new WP_Query( $args );

    var_dump( $allsearch );
|improve this answer|||||
  • IT doesn't return any articles by the authors. It just returns articles related to the search. Also what's the point of the printer() and adding it as an action? – Wold Oct 29 '14 at 3:33
  • That's just a way of doing an exemple the printer, and there was a bug on my code I found and Fixed it. – Bordoni Oct 29 '14 at 3:46
  • Just to clarify: I want either posts that have the keyword in their content, or posts by the author. Obviously, the posts by the author don't have to have the keyword in them. I"m not sure that this does that right? This just changes all instance of AND to OR right? – Wold Oct 29 '14 at 3:49
  • No, what I did there was changing the first instance of the AND to a OR for the search query; Which will make the SQL query for either the Author OR Search – Bordoni Oct 29 '14 at 3:51
  • So if it's not working, then there might be a problem with the author value? I made sure it was corect – Wold Oct 29 '14 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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