Title: query_posts join with custom table
Last modified: August 20, 2016

---

# query_posts join with custom table

 *  [_chris_](https://wordpress.org/support/users/_chris_/)
 * (@_chris_)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/query_posts-join-with-custom-table/)
 * Hi.
 * I have table in my DB called wp_bp_wpisy (post_id, group_id, date_added)
 * I need to get all posts with specific group_id, order this by date_added and 
   I need pagination for this.
 * I tried like this(found on forum):
 *     ```
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   
       $wpdb->bp_wpisy = $wpdb->prefix . 'bp_wpisy';
   
       function mam_posts_join ($join) {
          global $mam_global_join;
          if ($mam_global_join) $join .= " $mam_global_join";
          return $join;
       }
       function mam_posts_where ($where) {
          global $mam_global_where;
          if ($mam_global_where) $where .= " $mam_global_where";
          return $where;
       }
       function mam_posts_orderby ($orderby) {
          global $mam_global_orderby;
          if ($mam_global_orderby) $orderby = $mam_global_orderby;
          return $orderby;
       }
       add_filter('posts_join','mam_posts_join');
       add_filter('posts_where','mam_posts_where');
       add_filter('posts_orderby','mam_posts_orderby');
   
       $mam_global_join = " JOIN $wpdb->bp_wpisy AS wpisy ON $wpdb->posts.ID = wpisy.post_id  ";
       $mam_global_where = " and wpisy.group_id = 2";
       $mam_global_orderby = " wpisy.dodano ASC";
   
       query_posts("paged=$paged");
       /$mam_global_join = $mam_global_where = $mam_global_orderby = '';  // Turn off the filters
       ```
   
 * Can you help me ?

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

 *  [Namita](https://wordpress.org/support/users/namita/)
 * (@namita)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/query_posts-join-with-custom-table/#post-3376191)
 * Hi,
    Just did search for your query, you can refer to [http://wordpress.org/support/topic/left-join-with-query-post?replies=3](http://wordpress.org/support/topic/left-join-with-query-post?replies=3).
   i hope it will solve your issue.
 *  Thread Starter [_chris_](https://wordpress.org/support/users/_chris_/)
 * (@_chris_)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/query_posts-join-with-custom-table/#post-3376194)
 * as you can see I tried to rewrite that solution on my case but it doesn’t work.
   mayme i have made a mistake and I can’t see it
 *  [Namita](https://wordpress.org/support/users/namita/)
 * (@namita)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/query_posts-join-with-custom-table/#post-3376198)
 * can you please write the same code as per given on that link “[http://wordpress.org/support/topic/left-join-with-query-post?replies=3.&#8221](http://wordpress.org/support/topic/left-join-with-query-post?replies=3.&#8221);
   and replace the your filteration requirement. I have run the same code and working
   fine for me.
 *  Thread Starter [_chris_](https://wordpress.org/support/users/_chris_/)
 * (@_chris_)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/query_posts-join-with-custom-table/#post-3376219)
 * But code in #1 post is the same as in [http://wordpress.org/support/topic/left-join-with-query-post?replies=3](http://wordpress.org/support/topic/left-join-with-query-post?replies=3)
   but with my table and values

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

The topic ‘query_posts join with custom table’ is closed to new replies.

## Tags

 * [join](https://wordpress.org/support/topic-tag/join/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [_chris_](https://wordpress.org/support/users/_chris_/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/query_posts-join-with-custom-table/#post-3376219)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
