0

i need to excute posts from 2 search keywords & put all in one loop to showing by using WP-Query i have trying array_merge but this not worked for me so my last code is:

please note: i need to excute posts from 2 seprated keywords not one keyword containing 2 words & put the posts from keyword1, keyword2 in the same loop

$argssss=array('s'         => 'keyword1');
$argsss=array(
's'         => 'keyword2',
'orders'    => 'DESC',
 'showposts' => 60
);
$argss=array_merge( $argssss, $argsss );    
$queryy = new WP_Query($argss);

if ( $queryy->have_posts() ) :
while ( $queryy->have_posts() ) :
    $queryy->the_post();
    $do_not_duplicate = $post->ID;
    the_title( '<p>', '</p>' );
endwhile;

wp_reset_postdata();
endif;    

i have also tried to put s in array but not worked like this array('keyword1','keyword2')

so how can i excute posts from 2 different keywords

this answer is very very good it solved my problem by merging 2 wp_query in one Combining queries with different arguments per post type

another note this answer is very very good because it is really merged the two wp_query in one but i can't figure it with my filter Custom query with category exclusion and post-meta "whitelist"

but how can i use pagenavi with my custom query

  • well why dont you just keep one array and use s like "s"=>$keyword1 . " " . $keyword2 – Hasin Hayder Oct 26 '13 at 18:40
  • "s"=>keyword1 . " " . keyword2 , unfortunately not worked – adnan Oct 27 '13 at 7:06
  • but how can i use pagenavi with my custom query should probably be it's own question as it is unrelated to your first issue. And the answer (or link to it) is in the WP_Pagnavi FAQ anyway. – helgatheviking Oct 27 '13 at 12:15
4

array_merge does not work the way you expect/need it to, but honestly I am not sure why you are making this so complicated. All you need is:

$args = array(
  's'         => 'keyword1 keyword2',
  'orders'    => 'DESC',
  'showposts' => 60
);
// var_dump($args); // debug
$query = new WP_Query($args);
var_dump($query->request); // debug

The built is search parser will break the s string on the spaces and construct a functional query for the terms.

  • +1 - perfect answer - WP_Query takes care of the OR for all keywords listed. – cale_b Oct 26 '13 at 19:35
  • yasterday i have tried your code without var_dump not worked, now i have tried var_dump & recieved this error "string(309) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND ((wp_posts.post_title LIKE '%keyword1%') AND (wp_posts.post_title LIKE '%keyword2%')) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 60" – adnan Oct 27 '13 at 6:35
  • i have recieved 2 posts with this error every post is containing the 2 keywords – adnan Oct 27 '13 at 7:08
  • The var_dump data is not an error. It is print-out of the actual SQL used by the query. It is debugging information. That is why there is a comment next to that line that reads debug. Comment or remove that line in your final working code. As for your second comment... no idea what that means. – s_ha_dum Oct 27 '13 at 11:12
  • you are right this is not error sorry, i have achived my goal & updated the question, now i need to use pagenavi with my wp_query – adnan Oct 27 '13 at 11:53

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.