I don't know if I'm retarded, crazy or just plain genius or not, but I've decided to use tags as a way to link content site-wide (a big ol' many-to-many relationship website for archiving various aspects of a huge project). Unfortunately, this means that some custom post types utilised by some plugins don't exactly link up properly with other objects (posts, pages, etc.) as they use other taxonomies (and in the case of media 'attachments' they don't use taxonomies at all, unless I pork in a plugin to fix that -- which there is and I'm using it and it uses a different taxonomy...). Actually, some plugin types have specific taxonomies hard-coded in so I can't really go through and change their core code or else a plugin upgrade would wipe all my hard work.

To address this fragmentation and perception of long, arduous work of hacking the plugins and/or Wordpress, I initially used the Advanced Taxonomy stuff with WP_Query and that was working well, until I found I needed some extra information with meta tags and to even change the default ordering based on the post_type and meta_values specific to the post_type. So now I'm going the shorter "long, arduous" work route and have written my own custom SQL builder to fetch the objects based on the tag/meta relationships with the custom ordering per post_type as required by the project.

One thing I'd love to do, so I can use both this new custom query and the existing WP_Query methods, is find out some way to wrap the custom query results in a WP_Query class so I can use have_posts and the_post -- basically all the nice $post stuff. $wpdb->get_results only gives me a plain array of results and I'm not so clued up on WP_Query and $wpdb's niggly bits; can WP_Query let me do some funky custom selects, joins and then order based on the columns selected? Can I just flick an SQL query to get_posts() to get the WP_Query wrapper and its glorious Loop functionality?

It's late and I'm oh-so-close to solving this riddle, so I'd appreciate your help, Obi Wan Kenobi; For the moment the simplest solution looks to be the have_posts() functionality applied to a plain array of post results retrieved via the $wpdb->get_results function, to then easily and seamlessly initiate a WP Loop on that set of post results. If you may know of any other way to do this (Custom Queries, mayhaps?) I'm all ears.

2 Answers 2


setup_postdata will let you use loop functions with post results via get_posts.

another option is to use the various filters available to alter WP_Query's behavior- posts_join, posts_where, posts_orderby.


Ooh, I think I may have figured it out; I just stumbled upon it now so it's still early days. I'm trying to do some cheeky stuff so it's pretty minimal at the moment but should serve as a possible foundation:

function get_posts_custom_query( $query_args ) {
    global $wpdb;

    // Do the necessary funky stuff here to build $sql_query from the given $query_args
    $custom_query = $wpdb->get_results($sql_query);

    // If you've limited the results returned and using SQL_CALC_FOUND_ROWS in the select query...
    if ( $query_args['posts_per_page'] > 1 ) {
        $found_posts = $wpdb->get_var('SELECT FOUND_ROWS()');
        $max_num_pages = ceil($found_posts/$query_args['posts_per_page']);

    // Sanitise each post result
    foreach( $custom_query as $i => $post ) {
        $custom_query[$i] = sanitize_post($post, 'raw');

    // Setup WP_Query object
    $new_wp_query = new WP_Query();
    $new_wp_query->query = $sql_query;
    $new_wp_query->posts = $custom_query;
    $new_wp_query->post_count = count($custom_query);
    if ( isset($found_posts) ) $new_wp_query->found_posts = $found_posts;
    if ( isset($max_num_pages) ) $new_wp_query->max_num_pages = $max_num_pages;

    // Set the first post
    if ( $new_wp_query->post_count > 0 )
        $new_wp_query->post = $custom_query[0];

    return $new_wp_query;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.