WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Lets say I have a custom query which based on two custom post types:

$wp_query = new WP_Query(array(
           'post_type' => array('post-type-1', 'post-type-2'), 
           'post_status' => 'publish', 
           'posts_per_page' => '3');

what I want is to put posts from post-type-2 post on the third position in results on each page.

Is it possible in ONE query with filters or any other way ?

share|improve this question
You want two posts of post-type-1 followed by one post of post-type-2? Do you need to paginate the results? – s_ha_dum Nov 24 '12 at 16:13
@s_ha_dum yes, I need to paginate them. – Robert Isaev Nov 26 '12 at 3:42

Pagination should be easy. You really just need to add pagination parameters to your query, and next/previous posts links, more or less like in another thread I posted in recently.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query( 
        'paged'    => $paged 
// run the loop then, 
// pagination links ...
var_dump(paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'prev_text' => __('« Previous'),
    'next_text' => __('Next »'),
    'current' => max( 1, get_query_var('paged') ),
    'total' => $my_query->max_num_pages
) ));

But... getting a two to one ratio in every result set isn't.

I believe that I could pull this off with a custom SQL query (using a UNION) and $wpdb->get_results or $wpdb->get_col. However, the pagination would be a little tricky and you don't get WordPress post objects that way so chances are you'd need a second query to create those (and you nearly always need them). The minimal SQL would look something like...

(SELECT ID FROM {$wpdb->posts} WHERE post_type='post-type-1' LIMIT a,b) UNION ALL (SELECT ID FROM {$wpdb->posts} WHERE post_type='post-type-2' LIMIT c,d)

The tricky part is getting the values for those LIMITs right.

In principle, with the right filters-- posts_where, posts_fields, etc.-- you should be able to alter a query to match the custom SQL alluded to above, but again the pagination would be a little tricky and the actual query that WP_Query uses is more complicated so you'd have to match that when you build the UNION.

With either of those solutions it might be necessary to sort the results before echoing them. I'd have to set up the queries, test it, and see.

Ultimately, I can't convince myself that that either of those solutions are better than the simple and obvious one of just running two queries. I understand that you want to keep the query counts down. So do I, but sometimes two queries is the right move. Unless your server is severely underpowered or your site very badly designed in many other ways, two queries vs one for this page won't make a difference.


Is it possible in ONE query with filters or any other way ?

Yes, I think it is possible but you would have to use filters to radically alter the query and I don't think it is worth the time it would take to develop the solution and I don't think it is worth the saving in execution time that you may or may not get out of it.

share|improve this answer
Thanks for your answer, s_ha_dum. finally I came to this solution: each time post-type-1 updates I update post-type-2 posts date to keep them in the right position. – Robert Isaev Nov 29 '12 at 6:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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