WordPress “s” query with a few keywords in $args

I have this query that works fine:

 $query = new WP_Query(array( 's' => $keyword ,'post_status' => array('publish', 'pending', 'draft')) );

Its searches and finds the post with the keyword in title.

Read More

I want to query posts with a few keywords at once, (Now i’m doing a loop over all keywords and query each keyword by itself.

If it was mysql it would have be something like:

Select * from ..... where title='keyword1' or title='keyword2' or title='keyword3' ;

How could achieve that with WP_Query?

Related posts

2 comments

  1. You can’t do it using the WP_Query directly.

    An alternative for looping the WP_Query is use the $wpdb.

    $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE where title='keyword1' or title='keyword2' or title='keyword3'" );
    
  2. U have to use $wpdb (@codex)

    $query= $wpdb->get_results( $wpdb->prepare( 
        "SELECT * FROM ....
            WHERE title= %s OR
            title=%s OR
            title = %s
        ", 
        $keyword1,
        $keyword2,
        $keyword3
    ) );
    

    I hope that help u:)

Comments are closed.