Title: Custom SQL Query for Relating data
Last modified: August 20, 2016

---

# Custom SQL Query for Relating data

 *  [timfarley](https://wordpress.org/support/users/timfarley/)
 * (@timfarley)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/custom-sql-query-for-relating-data/)
 * Okay, I’ve got a long one…Here’s what we’re trying to do: We’re running Buddypress
   with a few custom post types (Book, URL) which users can add through a form on
   the front end. When a new Book is added, the Title, Author, Unique ID of Book,
   User ID of the person who added it, and a few other fields are added as metadata
   and custom taxonomies. If a new user attempts to add a book which someone else
   has already added, it doesn’t create a new post, but instead just adds their 
   User ID to the list of Readers (which is a custom taxonomy).
 * Each user has their own individual “Shelf” of items that they’ve added, however
   to display this we are simply creating a custom WP Query instance which looks
   for any posts with a specific User ID in the Readers taxonomy. All this happens
   perfectly, but the issue has to do with the order of the books on each individual
   user’s shelf. Right now, it is sorted by default (publish date) so if you add
   a new book, it shows up most recently in your queue. But if you “add” a book 
   that has already been added by someone else (so you’ve really just updated a 
   pre-existing Post), this book will appear in the queue in relation to its ORIGINAL
   publish date, not the date that you’ve added it.
 * Okay, thanks for sticking with me, here is where we need some help: We’d like
   to add some kind of metadata to each post to keep track of the position in a 
   specific user’s queue. One way I was considering to do this would be to create
   a new table and relate the User ID with the Post ID (the book) and then add a
   Sequence number which would represent where in the person’s queue the book should
   show up. However, my knowledge of SQL is very lacking and not sure how to tie
   in with WP’s pre-existing query methodology. Anyone think they can point me in
   the right direction?

Viewing 1 replies (of 1 total)

 *  [tasty.donuts](https://wordpress.org/support/users/tastydonuts/)
 * (@tastydonuts)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/custom-sql-query-for-relating-data/#post-2252806)
 * I’m not 100% sure on this so don’t get hung up in the woods, but where I would
   start would be to use the wpdb object … possibly calling it in your functions.
   php and using wp’s add function to attach it to the hooks made available in the
   wp posting system.
 * [http://codex.wordpress.org/Class_Reference/wpdb](http://codex.wordpress.org/Class_Reference/wpdb)

Viewing 1 replies (of 1 total)

The topic ‘Custom SQL Query for Relating data’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [tasty.donuts](https://wordpress.org/support/users/tastydonuts/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/custom-sql-query-for-relating-data/#post-2252806)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
