Title: complex wpdb query
Last modified: August 19, 2016

---

# complex wpdb query

 *  Resolved [ibraheemhsn](https://wordpress.org/support/users/ibraheemhsn/)
 * (@ibraheemhsn)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/complex-wpdb-query/)
 * hello every body
 * I need a query using $wpdb to do the following:
    ** return all published posts
   that belongs to category :’cat3′ AND have one or more of these tags [‘tag1′,’
   tag2′,’tag3’,,,,,,etc];
 * </string>
    thank you all

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/complex-wpdb-query/#post-1306121)
 * Does `query_posts('category_name=cat3&tag__in' => array(<ID1>,<ID2>)');` not 
   work?
 *  Thread Starter [ibraheemhsn](https://wordpress.org/support/users/ibraheemhsn/)
 * (@ibraheemhsn)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/complex-wpdb-query/#post-1306180)
 * thanks I will check it and replay again
 *  Thread Starter [ibraheemhsn](https://wordpress.org/support/users/ibraheemhsn/)
 * (@ibraheemhsn)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/complex-wpdb-query/#post-1306183)
 * sorry but this method casued for me unexpected errors ,because I want to use 
   secondary loop ,, using of $wpdb is more safety …
 * I read this qoute in in wp-codex [http://codex.wordpress.org/Template_Tags/query_posts](http://codex.wordpress.org/Template_Tags/query_posts)
 * > The query_posts function is intended to be used to modify the main page Loop
   > only. It is not intended as a means to create secondary Loops on the page. 
   > If you want to create separate Loops outside of the main one, you should create
   > separate WP_Query objects and use those instead. Use of query_posts on Loops
   > other than the main one can result in your main Loop becoming incorrect and
   > possibly displaying things that you were not expecting.
 * also I don’t want to use WP_QUERY object insted ,, I want to
    ** return all published
   posts that belongs to category :’cat3′ AND have one or more of these tags [‘tag1′,’
   tag2′,’tag3’,,,,,,etc]; using $wpdb
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/complex-wpdb-query/#post-1306202)
 * Ok, then use [this method](http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action)
   to create another loop. Then `print_r()` the object and you should see the query
   in the output. Copy that and use it for your own $wpdb query.
 * … not sure why you don’t want to use the query object though.
 *  Thread Starter [ibraheemhsn](https://wordpress.org/support/users/ibraheemhsn/)
 * (@ibraheemhsn)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/complex-wpdb-query/#post-1306323)
 * thanks MR.APLJDI
    that’s perfect .. the problem had been solved..
 * why should I don’t want to use wp_query and other builtin wp functions ?
    OK ,,**
   because I want to use another wordpress database as an external database and 
   use raw sql queries to retrive the informations from the external database**

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘complex wpdb query’ is closed to new replies.

## Tags

 * [advanced](https://wordpress.org/support/topic-tag/advanced/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * 5 replies
 * 2 participants
 * Last reply from: [ibraheemhsn](https://wordpress.org/support/users/ibraheemhsn/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/complex-wpdb-query/#post-1306323)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
