Title: Add custom query into &#8230;query?
Last modified: August 19, 2016

---

# Add custom query into …query?

 *  Resolved [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/add-custom-query-into-query/)
 * OMG – that’s just a bizarre question, I know. I wasn’t sure how to put it.
 * I was wanting to know if there was a way to “add in” a custom “orderby” into 
   a query_posts function. I’ve been trying to figure this out for weeks now, and
   I’m just at a loss.
 * yes, I know I can follow [this tutorial](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query),
   which I have tried to do it this way as well, but I’ve been unsuccessful thus
   far – and basically if I do that, I’m just reinventing the wheel.
 * What I need to do is order my posts – on my tag archive – by a meta value I’ve
   placed in the usermeta table. (I can get it to work with the codex page lined
   above, but I can’t get my tag archive to display correctly – I run into the same
   issue. I can order by the meta, but now my tag order won’t work – so one way 
   or another, something isn’t going right).
 * If I could *just* somehow pull in that meta value into the orderby, it would 
   solve all my problems.
 * Right now, I’m at this point:
 *     ```
       global $wpdb;
       $custom = "SELECT * FROM $wpdb->usermeta as um
                  LEFT JOIN $wpdb->users as u ON um.user_id = u.id
                  LEFT JOIN $wpdb->posts as p ON um.user_id = p.post_author
                  AND um.meta_key = 'article_position'
                  ORDER BY um.meta_value ASC";
   
       //now the query that puts it all together
            query_posts('tag='.$tagname.'&showposts=-1&author=-1'.$custom);
       ```
   
 * But of course it’s not working. I kind of didn’t expect it to, but right now 
   I’m to the point where I’m just trying *anything*. I’ve even messed with the 
   core query.php file – against my better judgement (it didn’t go anywhere, so 
   I changed it back.)
 * I keep thinking I’ve seen something like the above before – where you can “inject”
   a custom something-or-other in the query and it’ll still work, but for the life
   of me I can’t recall where. query_vars seems like it might be a possibility, 
   but I don’t see an example of it, and the definition doesn’t make it too clear
   to me as to what it does. (Actually, a LOT of stuff [on this page](http://codex.wordpress.org/Function_Reference/WP_Query)
   looks like, if I knew how to work with it, it might help – but alas I’m not sure
   what you do with it.)
 * Anyway, if someone can point me in the right direction, I’d sure appreciate it.

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

 *  Thread Starter [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/add-custom-query-into-query/#post-895146)
 * Oh yay! I found it!
 * I don’t know that it’ll work (yet) but it’s a point in the right direction, and
   that’s all I needed 🙂
 * The post that led me to (what I think is) the answer:
    [http://wordpress.org/support/topic/148005?replies=18#post-846120](http://wordpress.org/support/topic/148005?replies=18#post-846120)
 * And the “injection stuff” I was looking for:
    [http://codex.wordpress.org/Function_Reference/get_terms](http://codex.wordpress.org/Function_Reference/get_terms)
 * yee-haw – now off to play!
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/add-custom-query-into-query/#post-895157)
 * Hey d!
 * If I understand correctly, then Otto’s suggestion in this topic may help:
    [http://wordpress.org/support/topic/157515](http://wordpress.org/support/topic/157515)
 *  Thread Starter [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/add-custom-query-into-query/#post-895216)
 * Thanks Michael! I’ll look at that one too 🙂
 *  Thread Starter [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/add-custom-query-into-query/#post-895227)
 * Woo-hoo! Okay, thanks to that post, I also found [this post](http://wordpress.org/support/topic/169809?replies=2),
   which helped *immensely*. (ahh…Kaf. is there *nothing* you cannot do?)
 * I’ve got it working like a charm now. Thank you!

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

The topic ‘Add custom query into …query?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * Last activity: [17 years, 6 months ago](https://wordpress.org/support/topic/add-custom-query-into-query/#post-895227)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
